Repository: hashicorp/vault
File: ui/app/components/mount-backend-form.ts (TypeScript)

Code before:
  constructor(owner: unknown, args: Args) {
    super(owner, args);

    if (!this.args.mountType) {
      debug(`'mountType' is required to be passed into the component. Must return "auth" or "secret".`);
    }
  }

  willDestroy() {
    // components are torn down after store is unloaded and will cause an error if attempt to unload record
    const noTeardown = this.store && !this.store.isDestroying;
    if (noTeardown && this.args?.mountModel?.isNew) {
      this.args.mountModel.unloadRecord();
    }
    super.willDestroy();
  }

  checkPathChange(backendType: string) {
    if (!backendType) return;
    const mount = this.args.mountModel;
    const currentPath = mount.path;
    // while mountType is always set to 'secret' or 'auth', there's situations where we pass in an empty string (such as on setMountType) and in this case we want to default to returning auth methods.
    const mountsByType = filterEnginesByMountType(
      this.args.mountType ? this.args.mountType : 'auth',
      true
    ).map((engine) => engine.type);

    // if the current path has not been altered by user,
    // change it here to match the new type
    if (!currentPath || mountsByType.includes(currentPath)) {
      mount.path = backendType;
    }
  }

  typeChangeSideEffect(type: string) {
    if (this.args.mountType !== 'secret') return;
    if (type === 'pki') {
      // If type PKI, set max lease to ~10years
      this.args.mountModel.config.maxLeaseTtl = '3650d';
    } else {
      // otherwise reset
      this.args.mountModel.config.maxLeaseTtl = 0;
    }
  }

  checkModelValidity(model: MountModel) {
    const { isValid, state, invalidFormMessage } = model.validate();
    this.modelValidations = state;
    this.invalidFormAlert = invalidFormMessage;
    return isValid;
  }

Review comment (security):
We can use` ??`, which returns the right-hand value **only if** the left-hand value is null or undefined.
Also, I've made `filterEnginesByMountType` use named params, which makes it easier for a user to understand what we're passing in. In this example, it clarifies what "true" indicates.
```suggestion
    const mountsByType = filterEnginesByMountType({
      mountType = this.args.mountType ??  'auth',
      includeEnterprise = true
    }).map((engine) => engine.type);
```

Code after:

  constructor(owner: unknown, args: Args) {
    super(owner, args);

    assert(`@mountCategory is required. Must be "auth" or "secret".`, presence(this.args.mountCategory));
  }

  willDestroy() {
    // components are torn down after store is unloaded and will cause an error if attempt to unload record
    const noTeardown = this.store && !this.store.isDestroying;
    if (noTeardown && this.args?.mountModel?.isNew) {
      this.args.mountModel.unloadRecord();
    }
    super.willDestroy();
  }

  checkPathChange(backendType: string) {
    if (!backendType) return;
    const mount = this.args.mountModel;
    const currentPath = mount.path;
    // mountCategory is usually 'secret' or 'auth', but sometimes an empty string is passed in (like when we click the cancel button).
    // In these cases, we should default to returning auth methods.
    const mountsByType = filterEnginesByMountCategory({
      mountCategory: this.args.mountCategory ?? 'auth',
      isEnterprise: true,
    }).map((engine) => engine.type);
    // if the current path has not been altered by user,
    // change it here to match the new type
    if (!currentPath || mountsByType.includes(currentPath)) {
      mount.path = backendType;
    }
  }

  typeChangeSideEffect(type: string) {
    if (this.args.mountCategory !== 'secret') return;
    if (type === 'pki') {
      // If type PKI, set max lease to ~10years
      this.args.mountModel.config.maxLeaseTtl = '3650d';
    } else {
      // otherwise reset
      this.args.mountModel.config.maxLeaseTtl = 0;
    }
  }

  checkModelValidity(model: MountModel) {
    const { isValid, state, invalidFormMessage } = model.validate();
    this.modelValidations = state;
    this.invalidFormAlert = invalidFormMessage;
    return isValid;
  }