UNPKG

ottoman

Version:
34 lines 1.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.applyValidator = void 0; const schema_1 = require("../schema"); const errors_1 = require("../errors"); const applyValidator = (val, validator, name) => { let _validator = undefined; if (validator !== undefined) { switch (typeof validator) { case 'string': if (typeof schema_1.Schema.validators[validator] === 'undefined') { throw new errors_1.BuildSchemaError(`Validator '${validator}' for field '${name}' does not exist.`); } _validator = schema_1.Schema.validators[validator]; break; case 'function': _validator = validator; break; case 'object': if (!validator.regexp !== undefined) { _validator = (value) => { if (!validator.regexp.test(String(value))) { throw new errors_1.ValidationError(validator.message); } }; } } if (_validator !== undefined) { _validator(val); } } }; exports.applyValidator = applyValidator; //# sourceMappingURL=validator.js.map