UNPKG

@athenna/validator

Version:

The Athenna validation solution. Built on top of VineJS.

156 lines (155 loc) 5.22 kB
/** * @athenna/validator * * (c) João Lenon <lenon@athenna.io> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ /* eslint-disable no-use-before-define */ import { v, Vine, VineAccepted, VineAny, VineArray, VineBoolean, VineDate, VineEnum, VineLiteral, VineNumber, VineObject, VineRecord, VineString, VineTuple } from '#src'; import { Macroable } from '@athenna/common'; import { SimpleMessagesProvider } from '@vinejs/vine'; export class ValidatorImpl extends Macroable { /** * This getter will return the vine instance to * build your validation schemas: * * ```ts * const schema = v.object({ * name: v.string(), * email: v.string().email(), * password: v.string().minLength(8).maxLength(32).confirmed() * }) * ``` */ get schema() { return v; } /** * Validate data by passing a schema and data. Also * accepts other fields such as message provider * and error reporter. * * ```ts * const data = { name: 'Lenon' } * const schema = v.object({ name: v.string() }) * * await v.validate({ schema, data }) * ``` */ async validate(options) { return v.validate(options); } /** * Extend vine validation schema by adding new * validation rules or add custom messages: * * ```ts * Validate.extend().string('lenon', (value, options, field) => { * if (!Is.String(value)) { * return * } * * if (value !== 'lenon') { * field.report('The {{ field }} field value is not lenon', 'lenon', field) * } * }) * ``` */ extend() { const macro = (Vine, name, handler) => { Vine.macro(name, handler); }; return { messages: (messages) => { v.messagesProvider = new SimpleMessagesProvider(messages); return this; }, accepted: (name, handler) => { macro(VineAccepted, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, date: (name, handler) => { macro(VineDate, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, record: (name, handler) => { macro(VineRecord, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, tuple: (name, handler) => { macro(VineTuple, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, literal: (name, handler) => { macro(VineLiteral, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, array: (name, handler) => { macro(VineArray, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, any: (name, handler) => { macro(VineAny, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, string: (name, handler) => { macro(VineString, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, number: (name, handler) => { macro(VineNumber, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, enum: (name, handler) => { macro(VineEnum, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, boolean: (name, handler) => { macro(VineBoolean, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; }, object: (name, handler) => { macro(VineObject, name, function (opt) { const rule = v.createRule(handler); return this.use(rule(opt)); }); return this; } }; } }