kia-ml
Version:
Kia
62 lines (53 loc) • 1.48 kB
JavaScript
const ValidateJS = require('validate.js'); // HACK(steve): wierd typescript situation because of strange typings
const Validate = ValidateJS.default ? ValidateJS.default : ValidateJS;
/**
* Validates that 1 attribute doesn't appear in another's attributes content.
*/
Validate.validators.excludes = function custom(value, options, _key, attributes) {
const list = attributes[options.attribute] || [];
if (value && list.includes(value)) {
return options.message || `${value} is in the list`;
}
};
/**
* Validates that another attribute isn't true.
*/
Validate.validators.tripped = function custom(value, options, _key, attributes) {
if (value && attributes[options.attribute] === true) {
return options.message || `${options.attribute} is true`;
}
};
/**
* Defines the rules for validating.
*
* Example:
* ```ts
* const RULES = {
* favoriteBand: {
* inclusion: { ['Weezer', 'Other'], message: 'Pick wisely.' }
* },
* name: {
* presence: { message: 'A developer has no name?' }
* }
* }
* validate(RULES, {})
* ```
*
* See https://validatejs.org/#validators for more examples.
*
*/
/**
* Runs the given rules against the data object.
*
* @param rules The rules to apply.
* @param data The object to validate.
*/
export function validate(rules, data) {
if (typeof data !== 'object') {
return {};
}
return Validate(data, rules, {
fullMessages: false
}) || {};
}
//# sourceMappingURL=validate.js.map