@oscarpalmer/jhunal 0.25.0 → 0.27.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constants.d.mts +15 -7
- package/dist/constants.mjs +15 -6
- package/dist/handler/base.handler.d.mts +6 -0
- package/dist/{validator/base.validator.mjs → handler/base.handler.mjs} +5 -5
- package/dist/handler/function.handler.d.mts +6 -0
- package/dist/handler/function.handler.mjs +9 -0
- package/dist/handler/object.handler.d.mts +7 -0
- package/dist/handler/object.handler.mjs +130 -0
- package/dist/handler/schema.handler.d.mts +7 -0
- package/dist/handler/schema.handler.mjs +16 -0
- package/dist/handler/type.handler.d.mts +9 -0
- package/dist/handler/type.handler.mjs +71 -0
- package/dist/handler/validator.handler.d.mts +10 -0
- package/dist/handler/validator.handler.mjs +34 -0
- package/dist/handler/value.handler.d.mts +14 -0
- package/dist/handler/value.handler.mjs +98 -0
- package/dist/helpers/message.helper.d.mts +9 -7
- package/dist/helpers/message.helper.mjs +34 -16
- package/dist/helpers/misc.helper.d.mts +13 -6
- package/dist/helpers/misc.helper.mjs +12 -4
- package/dist/helpers/report.helper.d.mts +23 -0
- package/dist/helpers/report.helper.mjs +19 -0
- package/dist/helpers/result.helper.d.mts +7 -0
- package/dist/helpers/result.helper.mjs +17 -0
- package/dist/index.d.mts +172 -73
- package/dist/index.mjs +396 -235
- package/dist/models/infer.model.d.mts +11 -8
- package/dist/models/misc.model.d.mts +8 -8
- package/dist/models/schematic.plain.model.d.mts +10 -9
- package/dist/models/schematic.typed.model.d.mts +1 -1
- package/dist/models/transform.model.d.mts +2 -2
- package/dist/models/validation.model.d.mts +56 -25
- package/dist/models/validation.model.mjs +11 -2
- package/dist/schema.d.mts +34 -34
- package/dist/schema.mjs +11 -19
- package/dist/validator.d.mts +83 -0
- package/dist/validator.mjs +25 -0
- package/package.json +2 -2
- package/src/constants.ts +30 -8
- package/src/{validator/base.validator.ts → handler/base.handler.ts} +6 -6
- package/src/handler/function.handler.ts +9 -0
- package/src/handler/object.handler.ts +245 -0
- package/src/handler/schema.handler.ts +25 -0
- package/src/handler/type.handler.ts +160 -0
- package/src/handler/validator.handler.ts +49 -0
- package/src/handler/value.handler.ts +202 -0
- package/src/helpers/message.helper.ts +72 -30
- package/src/helpers/misc.helper.ts +23 -6
- package/src/helpers/report.helper.ts +72 -0
- package/src/helpers/result.helper.ts +33 -0
- package/src/index.ts +1 -0
- package/src/models/infer.model.ts +31 -13
- package/src/models/misc.model.ts +9 -9
- package/src/models/schematic.plain.model.ts +12 -9
- package/src/models/schematic.typed.model.ts +3 -3
- package/src/models/transform.model.ts +2 -2
- package/src/models/validation.model.ts +75 -37
- package/src/schema.ts +44 -70
- package/src/validator.ts +135 -0
- package/dist/validator/base.validator.d.mts +0 -6
- package/dist/validator/function.validator.d.mts +0 -6
- package/dist/validator/function.validator.mjs +0 -9
- package/dist/validator/named.handler.d.mts +0 -6
- package/dist/validator/named.handler.mjs +0 -23
- package/dist/validator/named.validator.d.mts +0 -7
- package/dist/validator/named.validator.mjs +0 -38
- package/dist/validator/object.validator.d.mts +0 -7
- package/dist/validator/object.validator.mjs +0 -185
- package/dist/validator/schematic.validator.d.mts +0 -7
- package/dist/validator/schematic.validator.mjs +0 -16
- package/src/validator/function.validator.ts +0 -9
- package/src/validator/named.handler.ts +0 -65
- package/src/validator/named.validator.ts +0 -61
- package/src/validator/object.validator.ts +0 -366
- package/src/validator/schematic.validator.ts +0 -25
|
@@ -1,366 +0,0 @@
|
|
|
1
|
-
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
-
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
3
|
-
import {join} from '@oscarpalmer/atoms/string';
|
|
4
|
-
import {clone} from '@oscarpalmer/atoms/value/clone';
|
|
5
|
-
import {
|
|
6
|
-
PROPERTY_DEFAULT,
|
|
7
|
-
PROPERTY_REQUIRED,
|
|
8
|
-
PROPERTY_TYPE,
|
|
9
|
-
PROPERTY_VALIDATORS,
|
|
10
|
-
SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY,
|
|
11
|
-
TYPE_ALL,
|
|
12
|
-
TYPE_UNDEFINED,
|
|
13
|
-
} from '../constants';
|
|
14
|
-
import {
|
|
15
|
-
getDefaultRequiredMessage,
|
|
16
|
-
getDefaultTypeMessage,
|
|
17
|
-
getDisallowedMessage,
|
|
18
|
-
getInputPropertyMissingMessage,
|
|
19
|
-
getInputPropertyTypeMessage,
|
|
20
|
-
getInputTypeMessage,
|
|
21
|
-
getRequiredMessage,
|
|
22
|
-
getSchematicPropertyNullableMessage,
|
|
23
|
-
getSchematicPropertyTypeMessage,
|
|
24
|
-
getUnknownKeysMessage,
|
|
25
|
-
} from '../helpers/message.helper';
|
|
26
|
-
import {getParameters, isSchema} from '../helpers/misc.helper';
|
|
27
|
-
import type {ValueName} from '../models/misc.model';
|
|
28
|
-
import {
|
|
29
|
-
type NamedValidatorHandlers,
|
|
30
|
-
SchematicError,
|
|
31
|
-
ValidationError,
|
|
32
|
-
type ValidationInformation,
|
|
33
|
-
type ValidationInformationKey,
|
|
34
|
-
type Validator,
|
|
35
|
-
type ValidatorDefaults,
|
|
36
|
-
type ValidatorItem,
|
|
37
|
-
type ValidatorType,
|
|
38
|
-
} from '../models/validation.model';
|
|
39
|
-
import {getBaseValidator} from './base.validator';
|
|
40
|
-
import {getFunctionValidator} from './function.validator';
|
|
41
|
-
import {getNamedHandlers} from './named.handler';
|
|
42
|
-
import {getNamedValidator} from './named.validator';
|
|
43
|
-
import {getSchemaValidator} from './schematic.validator';
|
|
44
|
-
|
|
45
|
-
function getDefaults(
|
|
46
|
-
obj: PlainObject,
|
|
47
|
-
key: string,
|
|
48
|
-
allowed: boolean,
|
|
49
|
-
): ValidatorDefaults | undefined {
|
|
50
|
-
if (!(PROPERTY_DEFAULT in obj)) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
if (!allowed) {
|
|
55
|
-
throw new SchematicError(getDisallowedMessage(key, PROPERTY_DEFAULT));
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return {
|
|
59
|
-
value: obj[PROPERTY_DEFAULT],
|
|
60
|
-
};
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function getDisallowedProperty(obj: PlainObject): string | undefined {
|
|
64
|
-
if (PROPERTY_DEFAULT in obj) {
|
|
65
|
-
return PROPERTY_DEFAULT;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
if (PROPERTY_REQUIRED in obj) {
|
|
69
|
-
return PROPERTY_REQUIRED;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
if (PROPERTY_TYPE in obj) {
|
|
73
|
-
return PROPERTY_TYPE;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (PROPERTY_VALIDATORS in obj) {
|
|
77
|
-
return PROPERTY_VALIDATORS;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
export function getObjectValidator(
|
|
82
|
-
original: PlainObject,
|
|
83
|
-
origin?: ValidationInformationKey,
|
|
84
|
-
fromType?: boolean,
|
|
85
|
-
): Validator {
|
|
86
|
-
const keys = Object.keys(original);
|
|
87
|
-
const keysLength = keys.length;
|
|
88
|
-
|
|
89
|
-
if (keysLength === 0) {
|
|
90
|
-
throw new SchematicError(SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
if (fromType ?? false) {
|
|
94
|
-
const property = getDisallowedProperty(original);
|
|
95
|
-
|
|
96
|
-
if (property != null) {
|
|
97
|
-
throw new SchematicError(getDisallowedMessage(origin!.full, property));
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const set = new Set<string>();
|
|
102
|
-
|
|
103
|
-
const items: ValidatorItem[] = [];
|
|
104
|
-
|
|
105
|
-
for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
106
|
-
const key = keys[keyIndex];
|
|
107
|
-
const value = original[key];
|
|
108
|
-
|
|
109
|
-
if (value == null) {
|
|
110
|
-
throw new SchematicError(getSchematicPropertyNullableMessage(join([origin?.full, key], '.')));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const prefixedKey = origin == null ? key : join([origin.full, key], '.');
|
|
114
|
-
|
|
115
|
-
const fullKey: ValidationInformationKey = {
|
|
116
|
-
full: prefixedKey,
|
|
117
|
-
short: key,
|
|
118
|
-
};
|
|
119
|
-
|
|
120
|
-
let handlers: NamedValidatorHandlers = {};
|
|
121
|
-
let required = true;
|
|
122
|
-
let typed = false;
|
|
123
|
-
|
|
124
|
-
let defaults: ValidatorDefaults | undefined;
|
|
125
|
-
|
|
126
|
-
let types: ValidatorType[];
|
|
127
|
-
|
|
128
|
-
const validators: Validator[] = [];
|
|
129
|
-
|
|
130
|
-
if (isPlainObject(value)) {
|
|
131
|
-
typed = PROPERTY_TYPE in value;
|
|
132
|
-
|
|
133
|
-
const type = typed ? value[PROPERTY_TYPE] : value;
|
|
134
|
-
|
|
135
|
-
defaults = getDefaults(value, prefixedKey, typed);
|
|
136
|
-
handlers = getNamedHandlers(value[PROPERTY_VALIDATORS], prefixedKey, typed);
|
|
137
|
-
required = getRequired(value, prefixedKey, typed) ?? required;
|
|
138
|
-
|
|
139
|
-
types = Array.isArray(type) ? type : [type];
|
|
140
|
-
} else {
|
|
141
|
-
types = Array.isArray(value) ? value : [value];
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
if (types.length === 0) {
|
|
145
|
-
throw new SchematicError(getSchematicPropertyTypeMessage(prefixedKey));
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
const typesLength = types.length;
|
|
149
|
-
|
|
150
|
-
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
151
|
-
const type = types[typeIndex];
|
|
152
|
-
|
|
153
|
-
let validator: Validator;
|
|
154
|
-
|
|
155
|
-
switch (true) {
|
|
156
|
-
case typeof type === 'function':
|
|
157
|
-
validator = getFunctionValidator(type);
|
|
158
|
-
break;
|
|
159
|
-
|
|
160
|
-
case isPlainObject(type):
|
|
161
|
-
validator = getObjectValidator(type, fullKey, typed);
|
|
162
|
-
break;
|
|
163
|
-
|
|
164
|
-
case isSchema(type):
|
|
165
|
-
validator = getSchemaValidator(type);
|
|
166
|
-
break;
|
|
167
|
-
|
|
168
|
-
case TYPE_ALL.has(type as ValueName):
|
|
169
|
-
validator = getNamedValidator(fullKey, type as ValueName, handlers);
|
|
170
|
-
break;
|
|
171
|
-
|
|
172
|
-
default:
|
|
173
|
-
throw new SchematicError(getSchematicPropertyTypeMessage(prefixedKey));
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
validators.push(validator);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
required = required && !types.includes(TYPE_UNDEFINED);
|
|
180
|
-
|
|
181
|
-
if (defaults != null && !required) {
|
|
182
|
-
throw new SchematicError(getDefaultRequiredMessage(prefixedKey));
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const validator = getBaseValidator(validators);
|
|
186
|
-
|
|
187
|
-
if (defaults != null && Array.isArray(validator(defaults.value, getParameters(), false))) {
|
|
188
|
-
throw new SchematicError(getDefaultTypeMessage(prefixedKey, types));
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
items.push({
|
|
192
|
-
defaults,
|
|
193
|
-
required,
|
|
194
|
-
types,
|
|
195
|
-
validator,
|
|
196
|
-
key: fullKey,
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
set.add(key);
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
const validatorsLength = items.length;
|
|
203
|
-
|
|
204
|
-
return (input, parameters, get) => {
|
|
205
|
-
if (!isPlainObject(input)) {
|
|
206
|
-
if (origin != null) {
|
|
207
|
-
return [];
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
const information: ValidationInformation = {
|
|
211
|
-
key: {
|
|
212
|
-
full: '',
|
|
213
|
-
short: '',
|
|
214
|
-
},
|
|
215
|
-
value: input,
|
|
216
|
-
message: getInputTypeMessage(input),
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
if (parameters.reporting.throw) {
|
|
220
|
-
throw new ValidationError([information]);
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
parameters.information?.push(information);
|
|
224
|
-
|
|
225
|
-
return [information];
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
if (parameters.strict) {
|
|
229
|
-
const inputKeys = Object.keys(input);
|
|
230
|
-
const unknownKeys = inputKeys.filter(key => !set.has(key));
|
|
231
|
-
|
|
232
|
-
if (unknownKeys.length > 0) {
|
|
233
|
-
const information: ValidationInformation = {
|
|
234
|
-
key: origin ?? {
|
|
235
|
-
full: '',
|
|
236
|
-
short: '',
|
|
237
|
-
},
|
|
238
|
-
message: getUnknownKeysMessage(unknownKeys),
|
|
239
|
-
value: input,
|
|
240
|
-
};
|
|
241
|
-
|
|
242
|
-
if (parameters.reporting.throw) {
|
|
243
|
-
throw new ValidationError([information]);
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
parameters.information?.push(information);
|
|
247
|
-
|
|
248
|
-
return [information];
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const allInformation: ValidationInformation[] = [];
|
|
253
|
-
const output: PlainObject = {};
|
|
254
|
-
|
|
255
|
-
for (let validatorIndex = 0; validatorIndex < validatorsLength; validatorIndex += 1) {
|
|
256
|
-
const {defaults, key, required, types, validator} = items[validatorIndex];
|
|
257
|
-
|
|
258
|
-
const value = (input as PlainObject)[key.short];
|
|
259
|
-
|
|
260
|
-
if (value === undefined) {
|
|
261
|
-
if (required) {
|
|
262
|
-
if (get && defaults != null) {
|
|
263
|
-
output[key.short] = clone(defaults.value);
|
|
264
|
-
|
|
265
|
-
continue;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
if (parameters.reporting.none) {
|
|
269
|
-
return [];
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
const information: ValidationInformation = {
|
|
273
|
-
key,
|
|
274
|
-
value,
|
|
275
|
-
message: getInputPropertyMissingMessage(key.full, types),
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
if (parameters.reporting.throw) {
|
|
279
|
-
throw new ValidationError([information]);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
parameters.information?.push(information);
|
|
283
|
-
|
|
284
|
-
if (parameters.reporting.all) {
|
|
285
|
-
allInformation.push(information);
|
|
286
|
-
|
|
287
|
-
continue;
|
|
288
|
-
}
|
|
289
|
-
|
|
290
|
-
return [information];
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
const previousOutput = parameters.output;
|
|
297
|
-
|
|
298
|
-
parameters.output = output;
|
|
299
|
-
|
|
300
|
-
const result = validator(value, parameters, get);
|
|
301
|
-
|
|
302
|
-
parameters.output = previousOutput;
|
|
303
|
-
|
|
304
|
-
if (result === true) {
|
|
305
|
-
if (get && !isPlainObject(value)) {
|
|
306
|
-
output[key.short] = parameters.clone ? clone(value) : value;
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
continue;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
if (parameters.reporting.none) {
|
|
313
|
-
return [];
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
const information: ValidationInformation[] =
|
|
317
|
-
typeof result !== 'boolean' && result.length > 0
|
|
318
|
-
? result
|
|
319
|
-
: [
|
|
320
|
-
{
|
|
321
|
-
key,
|
|
322
|
-
value,
|
|
323
|
-
message: getInputPropertyTypeMessage(key.full, types, value),
|
|
324
|
-
},
|
|
325
|
-
];
|
|
326
|
-
|
|
327
|
-
if (parameters.reporting.throw) {
|
|
328
|
-
throw new ValidationError(information);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
if (parameters.reporting.all) {
|
|
332
|
-
allInformation.push(...information);
|
|
333
|
-
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
return information;
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
if (get) {
|
|
341
|
-
if (origin == null) {
|
|
342
|
-
parameters.output = output;
|
|
343
|
-
} else {
|
|
344
|
-
parameters.output[origin.short] = output;
|
|
345
|
-
}
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
return allInformation.length === 0 ? true : allInformation;
|
|
349
|
-
};
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
function getRequired(obj: PlainObject, key: string, allowed: boolean): boolean | undefined {
|
|
353
|
-
if (!(PROPERTY_REQUIRED in obj)) {
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
if (!allowed) {
|
|
358
|
-
throw new SchematicError(getDisallowedMessage(key, PROPERTY_REQUIRED));
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
if (typeof obj[PROPERTY_REQUIRED] !== 'boolean') {
|
|
362
|
-
throw new SchematicError(getRequiredMessage(key));
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
return obj[PROPERTY_REQUIRED];
|
|
366
|
-
}
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
-
import type {Validator} from '../models/validation.model';
|
|
3
|
-
import {Schema, schemaValidators} from '../schema';
|
|
4
|
-
|
|
5
|
-
export function getSchemaValidator(schematic: Schema<unknown>): Validator {
|
|
6
|
-
const validator = schemaValidators.get(schematic)!;
|
|
7
|
-
|
|
8
|
-
return (input, parameters, get) => {
|
|
9
|
-
let result: ReturnType<Validator>;
|
|
10
|
-
|
|
11
|
-
if (isPlainObject(input)) {
|
|
12
|
-
result = validator(input, parameters, get);
|
|
13
|
-
} else {
|
|
14
|
-
result = [];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
if (result === true) {
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
parameters.information?.push(...result);
|
|
22
|
-
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
}
|