3172e74d7e
* KMSE: Key Model / Adapter / Serializer setup (#13638) * First pass model * KMS key adapter (create/update), serializer, model * Add last rotated and provider to key * KeyEdit secret-edit component, and more key model stuff * add formatDate param support to infotablerow * Add keymgmt key to routes and options-for-backend * Rename keymgmt-key to keymgmt/key * Add test, cleanup * Add mirage handler for kms * Address PR comments * KMS Providers (#13797) * adds pagination-controls component * adds kms provider model, adapter and serializer * adds kms provider-edit component * updates secrets routes to handle itemType query param for kms * updates kms key adapter to query by provider * adds tests for provider-edit component * refactors kms provider adapter to account for dynamic path * adds model-validations-helper util * removes keymgmt from supported-secret-backends * fixes issue generating url for fetching keys for a provider * updates modelType method on secret-edit route to accept options object as arg rather than transition * adds additional checks to ensure queryParams are defined in options object for modelType method * UI/keymgmt distribute key (#13840) * Add distribution details on key page, and empty states if no permissions * Allow search-select component to return object so parent can tell when new item was created * Add stringarray transform * Distribute component first pass * Refactor distribute component for use with internal object rather than ember-data model * Specific permission denied errors on key edit * Allow inline errors on search-select component * Style updates for form errors * Styling and error messages on distribute component * Allow block template on inline alert so we can add doc links * Add distribute action, flash messages, cleanup * Cleanup & Add tests * More cleanup * Address PR comments * Move disable operations logic to commponent class * KMSE Enable/Config (#14835) * adds keymgmt secrets engine as supported backend * adds comment to check on keymgmt as member of adp module * updates kms provider to use model-validations decorator * fixes lint errors and tests Co-authored-by: Chelsea Shaw <82459713+hashishaw@users.noreply.github.com>
120 lines
5.1 KiB
JavaScript
120 lines
5.1 KiB
JavaScript
/* eslint-disable no-console */
|
|
import validators from 'vault/utils/validators';
|
|
import { get } from '@ember/object';
|
|
|
|
/**
|
|
* used to validate properties on a class
|
|
*
|
|
* decorator expects validations object with the following shape:
|
|
* { [propertyKeyName]: [{ type, options, message, validator }] }
|
|
* each key in the validations object should refer to the property on the class to apply the validation to
|
|
* type refers to the type of validation to apply -- must be exported from validators util for lookup
|
|
* options is an optional object for given validator -- min, max, nullable etc. -- see validators in util
|
|
* message is added to the errors array and returned from the validate method if validation fails
|
|
* validator may be used in place of type to provide a function that gets executed in the validate method
|
|
* validator is useful when specific validations are needed (dependent on other class properties etc.)
|
|
* validator must be passed as function that takes the class context (this) as the only argument and returns true or false
|
|
* each property supports multiple validations provided as an array -- for example, presence and length for string
|
|
*
|
|
* validations must be invoked using the validate method which is added directly to the decorated class
|
|
* const { isValid, state } = this.model.validate();
|
|
* isValid represents the validity of the full class -- if no properties provided in the validations object are invalid this will be true
|
|
* state represents the error state of the properties defined in the validations object
|
|
* const { isValid, errors } = state[propertyKeyName];
|
|
* isValid represents the validity of the property
|
|
* errors will be populated with messages defined in the validations object when validations fail
|
|
* since a property can have multiple validations, errors is always returned as an array
|
|
*
|
|
*** basic example
|
|
*
|
|
* import Model from '@ember-data/model';
|
|
* import withModelValidations from 'vault/decorators/model-validations';
|
|
*
|
|
* const validations = { foo: [{ type: 'presence', message: 'foo is a required field' }] };
|
|
* @withModelValidations(validations)
|
|
* class SomeModel extends Model { foo = null; }
|
|
*
|
|
* const model = new SomeModel();
|
|
* const { isValid, state } = model.validate();
|
|
* -> isValid = false;
|
|
* -> state.foo.isValid = false;
|
|
* -> state.foo.errors = ['foo is a required field'];
|
|
*
|
|
*** example using custom validator
|
|
*
|
|
* const validations = { foo: [{ validator: (model) => model.bar.includes('test') ? model.foo : false, message: 'foo is required if bar includes test' }] };
|
|
* @withModelValidations(validations)
|
|
* class SomeModel extends Model { foo = false; bar = ['foo', 'baz']; }
|
|
*
|
|
* const model = new SomeModel();
|
|
* const { isValid, state } = model.validate();
|
|
* -> isValid = false;
|
|
* -> state.foo.isValid = false;
|
|
* -> state.foo.errors = ['foo is required if bar includes test'];
|
|
*/
|
|
|
|
export function withModelValidations(validations) {
|
|
return function decorator(SuperClass) {
|
|
return class ModelValidations extends SuperClass {
|
|
static _validations;
|
|
|
|
constructor() {
|
|
super(...arguments);
|
|
if (!validations || typeof validations !== 'object') {
|
|
throw new Error('Validations object must be provided to constructor for setup');
|
|
}
|
|
this._validations = validations;
|
|
}
|
|
|
|
validate() {
|
|
let isValid = true;
|
|
const state = {};
|
|
|
|
for (const key in this._validations) {
|
|
const rules = this._validations[key];
|
|
|
|
if (!Array.isArray(rules)) {
|
|
console.error(
|
|
`Must provide validations as an array for property "${key}" on ${this.modelName} model`
|
|
);
|
|
continue;
|
|
}
|
|
|
|
state[key] = { errors: [] };
|
|
|
|
for (const rule of rules) {
|
|
const { type, options, message, validator: customValidator } = rule;
|
|
// check for custom validator or lookup in validators util by type
|
|
const useCustomValidator = typeof customValidator === 'function';
|
|
const validator = useCustomValidator ? customValidator : validators[type];
|
|
if (!validator) {
|
|
console.error(
|
|
!type
|
|
? 'Validator not found. Define either type or pass custom validator function under "validator" key in validations object'
|
|
: `Validator type: "${type}" not found. Available validators: ${Object.keys(
|
|
validators
|
|
).join(', ')}`
|
|
);
|
|
continue;
|
|
}
|
|
const passedValidation = useCustomValidator
|
|
? validator(this)
|
|
: validator(get(this, key), options); // dot notation may be used to define key for nested property
|
|
|
|
if (!passedValidation) {
|
|
// consider setting a prop like validationErrors directly on the model
|
|
// for now return an errors object
|
|
state[key].errors.push(message);
|
|
if (isValid) {
|
|
isValid = false;
|
|
}
|
|
}
|
|
}
|
|
state[key].isValid = !state[key].errors.length;
|
|
}
|
|
return { isValid, state };
|
|
}
|
|
};
|
|
};
|
|
}
|