mirror of
https://github.com/hashicorp/vault.git
synced 2025-08-17 03:57:01 +02:00
* format readme to prepare for pattern info * small text changes * add markdown files for each section * readme updates * routing md draft * add table of contents * add oidc pr sample * update routing * add decorator section * serializer docs * add table of contents * update readme * add title * add decorator section * models readme * update comments * modify examples * add bullets and more comments * what the heck fix bullet * model docs * form docs * routing doc * serializer/adapter * adds docs for model-validations decorator (#20596) * UI Docs: Components (#20602) * Add CSS best practices (#20370) * wip--saving work * wip * friday morning.... * update * fix exists to exist * one more change * UI docs: Add ember engine creation documentation (#20789) --------- Co-authored-by: Jordan Reimer <zofskeez@gmail.com> Co-authored-by: Chelsea Shaw <82459713+hashishaw@users.noreply.github.com> Co-authored-by: Angel Garbarino <Monkeychip@users.noreply.github.com> Co-authored-by: Kianna <30884335+kiannaquach@users.noreply.github.com>
91 lines
3.3 KiB
JavaScript
91 lines
3.3 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
*/
|
|
|
|
/* eslint-disable no-console */
|
|
import validators from 'vault/utils/validators';
|
|
import { get } from '@ember/object';
|
|
|
|
// see documentation at ui/docs/model-validations.md for detailed usage information
|
|
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 = {};
|
|
let errorCount = 0;
|
|
|
|
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: [], warnings: [] };
|
|
|
|
for (const rule of rules) {
|
|
const { type, options, level, 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) {
|
|
// message can also be a function
|
|
const validationMessage = typeof message === 'function' ? message(this) : message;
|
|
// consider setting a prop like validationErrors directly on the model
|
|
// for now return an errors object
|
|
if (level === 'warn') {
|
|
state[key].warnings.push(validationMessage);
|
|
} else {
|
|
state[key].errors.push(validationMessage);
|
|
if (isValid) {
|
|
isValid = false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
errorCount += state[key].errors.length;
|
|
state[key].isValid = !state[key].errors.length;
|
|
}
|
|
|
|
return { isValid, state, invalidFormMessage: this.generateErrorCountMessage(errorCount) };
|
|
}
|
|
|
|
generateErrorCountMessage(errorCount) {
|
|
if (errorCount < 1) return null;
|
|
// returns count specific message: 'There is an error/are N errors with this form.'
|
|
const isPlural = errorCount > 1 ? `are ${errorCount} errors` : false;
|
|
return `There ${isPlural ? isPlural : 'is an error'} with this form.`;
|
|
}
|
|
};
|
|
};
|
|
}
|