UNPKG

@korbiniankuhn/validator

Version:

Validate object schemas for js, expressjs, angular and mongoose.

118 lines 4.43 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const any_1 = require("./types/any"); const message_1 = require("../../utils/message"); const options_1 = require("./options"); const lodash_1 = require("./../../utils/lodash"); const error_1 = require("./../../utils/error"); const boolean_1 = require("./types/boolean"); const string_1 = require("./types/string"); const number_1 = require("./types/number"); const date_1 = require("./types/date"); const array_1 = require("./types/array"); const object_1 = require("./types/object"); class Validator { constructor(options = {}) { if (!(this instanceof Validator)) { return new Validator(options); } this._options = Object.assign({}, options_1.VALIDATOR_OPTIONS, options); this._customs = {}; this._types = options_1.TYPES; this._message = new message_1.Message(lodash_1.defaultToAny(this._options.locale, 'en')); this._options.message = this._message; } validate(schema, data) { return __awaiter(this, void 0, void 0, function* () { if (!('validate' in schema)) { throw this._message.error('invalid_schema'); } try { return yield schema.validate(data); } catch (err) { const error = new error_1.ValidationError(this._message.get('validation_error'), err); if (this._options.throwValidationErrors) { throw error; } else { return error; } } }); } validateSync(schema, data) { if (!('validateSync' in schema)) { throw this._message.error('invalid_schema'); } try { return schema.validateSync(data); } catch (err) { const error = new error_1.ValidationError(this._message.get('validation_error'), err); if (this._options.throwValidationErrors) { throw error; } else { return error; } } } addLocale(name, messages) { this._message.addLocale(name, messages); return this; } setLocale(name) { this._message.setLocale(name); return this; } addType(name, schema) { if (name in this._customs) { throw this._message.error('duplicate_custom_type', { name }); } this._customs[name] = schema.clone(); return this; } Custom(name) { if (name in this._customs) { return this._customs[name].clone(); } else { throw this._message.error('unknown_custom_type', { name }); } } listCustomTypes() { return Object.keys(this._customs).map(key => `${key}: ${this._customs[key].constructor}`); } Any(options = {}) { return new any_1.AnySchema(options, this._options); } Array(schema, options = {}) { return new array_1.ArraySchema(schema, options, this._options); } Boolean(options = {}) { return new boolean_1.BooleanSchema(options, this._options); } Date(options = {}) { return new date_1.DateSchema(options, this._options); } Number(options = {}) { return new number_1.NumberSchema(options, this._options); } Object(schema = {}, options = {}) { return new object_1.ObjectSchema(schema, options, this._options); } String(options = {}) { return new string_1.StringSchema(options, this._options); } } exports.Validator = Validator; //# sourceMappingURL=validator.js.map