1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.systemValidators = void 0;
|
4 | const validationErrors = {
|
5 | string: {
|
6 | STRING_TYPE_VALIDATION_FAILED: (actualParam) => `"${actualParam}" should be of type string`,
|
7 | REGEX_VALIDATION_FAILED: (regex, actualParam) => `expected "${actualParam}" to match regex "${regex}"`,
|
8 | CONTAINS_VALIDATION_FAILED: (shouldContain, actualParam) => `expected "${actualParam}" to contain string "${shouldContain}"`,
|
9 | MIN_LENGTH_VALIDATION_FAILED: (length, actualParam) => `expected "${actualParam}" to be of length longer than or equal to ${length}`,
|
10 | MAX_LENGTH_VALIDATION_FAILED: (length, actualParam) => `expected "${actualParam}" to be of length shorter than or equal to ${length}`,
|
11 | UKNOWN_VALIDATOR: (name) => `encountered unknown string validator "${name}"`,
|
12 | },
|
13 | number: {
|
14 | NUMBER_TYPE_VALIDATION_FAILED: (actualParam) => `expected "${actualParam}" to be of type number`,
|
15 | MIN_VALIDATION_FAILED: (actualParam, min) => `expected "${actualParam}" to be larger than or equal to ${min}`,
|
16 | MAX_VALIDATION_FAILED: (actualParam, max) => `expected "${actualParam}" to be lesser then or equal to ${max}`,
|
17 | MULTIPLE_OF_VALIDATION_FAILED: (actualParam, multipleOf) => `expected "${actualParam}" to be a multiple of ${multipleOf}`,
|
18 | UKNOWN_VALIDATOR: (name) => `encountered unknown number validator "${name}"`,
|
19 | },
|
20 | enum: {
|
21 | ENUM_TYPE_VALIDATION_FAILED: (actualParam, options) => `expected "${actualParam}" to be one of the options: "${options.join(', ')}"`,
|
22 | NO_OPTIONS_DEFINED: () => `expected enum to be defined with one option or more`,
|
23 | },
|
24 | tag: {
|
25 | NO_SPACES_ALLOWED: (actualParam) => `expected "${actualParam}" to be a single value with no spaces`,
|
26 | },
|
27 | };
|
28 | exports.systemValidators = {
|
29 | string: {
|
30 | validate(value, validators, resolveParam, validateDefinition, validateValue) {
|
31 | const res = value;
|
32 | const errors = [];
|
33 | if (validateValue && typeof value !== 'string') {
|
34 | errors.push(validationErrors.string.STRING_TYPE_VALIDATION_FAILED(value));
|
35 | }
|
36 | if (validators.length > 0) {
|
37 | validators.forEach((validatorMeta) => {
|
38 | if (typeof validatorMeta === 'object') {
|
39 | if (this.subValidators && this.subValidators[validatorMeta.name]) {
|
40 | const subValidator = this.subValidators[validatorMeta.name];
|
41 | const validationRes = subValidator(value, resolveParam(validatorMeta.args[0]));
|
42 | if (validateValue && validationRes.errors) {
|
43 | errors.push(...validationRes.errors);
|
44 | }
|
45 | }
|
46 | else if (validateDefinition) {
|
47 | errors.push(validationErrors.string.UKNOWN_VALIDATOR(validatorMeta.name));
|
48 | }
|
49 | }
|
50 | });
|
51 | }
|
52 | return { res, errors: errors.length ? errors : null };
|
53 | },
|
54 | subValidators: {
|
55 | regex: (value, regex) => {
|
56 | const r = new RegExp(regex);
|
57 | const valid = r.test(value);
|
58 | return {
|
59 | res: value,
|
60 | errors: valid
|
61 | ? null
|
62 | : [validationErrors.string.REGEX_VALIDATION_FAILED(regex, value)],
|
63 | };
|
64 | },
|
65 | contains: (value, checkedValue) => {
|
66 | const valid = !!~value.indexOf(checkedValue);
|
67 | return {
|
68 | res: value,
|
69 | errors: valid
|
70 | ? null
|
71 | : [validationErrors.string.CONTAINS_VALIDATION_FAILED(checkedValue, value)],
|
72 | };
|
73 | },
|
74 | minLength: (value, length) => {
|
75 | const valid = value.length > Number(length);
|
76 | return {
|
77 | res: value,
|
78 | errors: valid
|
79 | ? null
|
80 | : [validationErrors.string.MIN_LENGTH_VALIDATION_FAILED(length, value)],
|
81 | };
|
82 | },
|
83 | maxLength: (value, length) => {
|
84 | const valid = value.length < Number(length);
|
85 | return {
|
86 | res: value,
|
87 | errors: valid
|
88 | ? null
|
89 | : [validationErrors.string.MAX_LENGTH_VALIDATION_FAILED(length, value)],
|
90 | };
|
91 | },
|
92 | },
|
93 | },
|
94 | number: {
|
95 | validate(value, validators, resolveParam, validateDefinition, validateValue) {
|
96 | const res = value;
|
97 | const errors = [];
|
98 | if (isNaN(value)) {
|
99 | if (validateValue) {
|
100 | errors.push(validationErrors.number.NUMBER_TYPE_VALIDATION_FAILED(value));
|
101 | }
|
102 | }
|
103 | else if (validators.length > 0) {
|
104 | validators.forEach((validatorMeta) => {
|
105 | if (typeof validatorMeta === 'object') {
|
106 | if (this.subValidators && this.subValidators[validatorMeta.name]) {
|
107 | const subValidator = this.subValidators[validatorMeta.name];
|
108 | const validationRes = subValidator(value, resolveParam(validatorMeta.args[0]));
|
109 | if (validateValue && validationRes.errors) {
|
110 | errors.push(...validationRes.errors);
|
111 | }
|
112 | }
|
113 | else if (validateDefinition) {
|
114 | errors.push(validationErrors.number.UKNOWN_VALIDATOR(validatorMeta.name));
|
115 | }
|
116 | }
|
117 | });
|
118 | }
|
119 | return { res, errors: errors.length ? errors : null };
|
120 | },
|
121 | subValidators: {
|
122 | min: (value, minValue) => {
|
123 | const valid = Number(value) > Number(minValue);
|
124 | return {
|
125 | res: value,
|
126 | errors: valid
|
127 | ? null
|
128 | : [validationErrors.number.MIN_VALIDATION_FAILED(value, minValue)],
|
129 | };
|
130 | },
|
131 | max: (value, maxValue) => {
|
132 | const valid = Number(value) < Number(maxValue);
|
133 | return {
|
134 | res: value,
|
135 | errors: valid
|
136 | ? null
|
137 | : [validationErrors.number.MAX_VALIDATION_FAILED(value, maxValue)],
|
138 | };
|
139 | },
|
140 | multipleOf: (value, multipleOf) => {
|
141 | const valid = Number(value) % Number(multipleOf) === 0;
|
142 | return {
|
143 | res: value,
|
144 | errors: valid
|
145 | ? null
|
146 | : [
|
147 | validationErrors.number.MULTIPLE_OF_VALIDATION_FAILED(value, multipleOf),
|
148 | ],
|
149 | };
|
150 | },
|
151 | },
|
152 | },
|
153 | enum: {
|
154 | validate(value, options, resolveParam, validateDefinition, validateValue) {
|
155 | const res = value;
|
156 | const errors = [];
|
157 | const stringOptions = [];
|
158 | if (options.length) {
|
159 | const isOneOf = options.some((option) => {
|
160 | if (typeof option === 'string') {
|
161 | stringOptions.push(option);
|
162 | return resolveParam(option) === value;
|
163 | }
|
164 | return true;
|
165 | });
|
166 | if (validateValue && !isOneOf) {
|
167 | errors.push(validationErrors.enum.ENUM_TYPE_VALIDATION_FAILED(value, stringOptions));
|
168 | }
|
169 | }
|
170 | else if (validateDefinition) {
|
171 | errors.push(validationErrors.enum.NO_OPTIONS_DEFINED());
|
172 | }
|
173 | return { res, errors: errors.length ? errors : null };
|
174 | },
|
175 | },
|
176 | tag: {
|
177 | validate(value, _options, _resolveParam, _validateDefinition, validateValue) {
|
178 | const errors = [];
|
179 | if (validateValue && ~value.indexOf(' ')) {
|
180 | errors.push(validationErrors.tag.NO_SPACES_ALLOWED(value));
|
181 | }
|
182 | return { res: value, errors: errors.length ? errors : null };
|
183 | },
|
184 | },
|
185 | };
|
186 |
|
\ | No newline at end of file |