1 | const messages = require('./messages/index');
|
2 | const rules = require('./rules/index');
|
3 | const Validator = require('./validator');
|
4 | const objUtil = require('./util/obj');
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | function setLang(lang) {
|
11 | messages.defaultLang = lang;
|
12 | }
|
13 |
|
14 | function extend(name, callback) {
|
15 | rules[name] = callback;
|
16 | }
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | function extendMessages(newMessages, lang = 'en') {
|
24 | if (typeof messages[lang] === 'undefined') {
|
25 | messages[lang] = {};
|
26 | }
|
27 | messages[lang] = Object.assign(messages[lang], newMessages);
|
28 | }
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | function addCustomMessages(customMessages, lang = 'en') {
|
36 | if (typeof messages[lang] === 'undefined') {
|
37 | messages[lang] = {};
|
38 | }
|
39 | messages[lang].$custom = Object.assign(messages[lang].$custom || {}, customMessages);
|
40 | }
|
41 |
|
42 | function niceNames(attributes, lang = 'en') {
|
43 | messages[lang].$niceNames = Object.assign(messages[lang].$niceNames || {}, attributes);
|
44 | }
|
45 |
|
46 |
|
47 | function koa() {
|
48 | return async (ctx, next) => {
|
49 |
|
50 | ctx.validationErrors = function validationErrors(errors) {
|
51 | return {
|
52 | body: {
|
53 | message: 'The given data is invalid.',
|
54 | errors,
|
55 | },
|
56 | };
|
57 | };
|
58 |
|
59 | ctx.validate = async function validate(rulesArray, inputs, useMessages) {
|
60 | const v = new Validator(
|
61 | inputs || { ...this.request.body, ...this.request.files },
|
62 | rulesArray || {},
|
63 | useMessages || {},
|
64 | );
|
65 |
|
66 | if (await v.fails()) {
|
67 | this.throw(422, this.validationErrors(v.errors));
|
68 | }
|
69 |
|
70 | return v;
|
71 | };
|
72 |
|
73 | ctx.validator = (inputs, rulesArray, useMessages) => new Validator(
|
74 | inputs || { ...this.request.body, ...this.request.files },
|
75 | rulesArray || {},
|
76 | useMessages || {},
|
77 | );
|
78 |
|
79 | try {
|
80 | await next();
|
81 | } catch (err) {
|
82 | if (err.status && err.status === 422) {
|
83 | ctx.type = 'json';
|
84 | ctx.status = 422;
|
85 | ctx.body = err.body;
|
86 | return;
|
87 | }
|
88 | throw err;
|
89 | }
|
90 | };
|
91 | }
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 | function bailable(sure) {
|
98 | Validator.bailable(sure);
|
99 | }
|
100 |
|
101 | function assert(validationRules) {
|
102 | Object.keys(validationRules).forEach((key) => {
|
103 | const rawRules = validationRules[key];
|
104 | let arrayRules = [];
|
105 | if (Array.isArray(rawRules)) {
|
106 | arrayRules = rawRules;
|
107 | } else {
|
108 | rawRules.split('|').forEach((raw) => {
|
109 | const [ruleName] = raw.split(':');
|
110 | arrayRules.push(ruleName);
|
111 | });
|
112 | }
|
113 | arrayRules.forEach((rule) => {
|
114 | if (typeof rules[rule] !== 'function') {
|
115 | throw new Error(`Rule ${rule} used for attribute ${key} is invalid.`);
|
116 | }
|
117 | });
|
118 | });
|
119 |
|
120 | return validationRules;
|
121 | }
|
122 |
|
123 | module.exports = {
|
124 | Validator,
|
125 | setLang,
|
126 | extend,
|
127 | extendMessages,
|
128 | addCustomMessages,
|
129 | niceNames,
|
130 | koa,
|
131 | bailable,
|
132 | assert,
|
133 | setStrNotationRepetition: objUtil.setStrNotationRepetition,
|
134 | };
|