@oscarpalmer/jhunal 0.24.0 → 0.26.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 +4 -4
- package/dist/constants.mjs +4 -4
- package/dist/helpers/misc.helper.d.mts +3 -3
- package/dist/helpers/misc.helper.mjs +3 -3
- package/dist/index.d.mts +64 -64
- package/dist/index.mjs +78 -56
- package/dist/models/infer.model.d.mts +21 -21
- package/dist/models/misc.model.d.mts +3 -3
- package/dist/models/{schema.plain.model.d.mts → schematic.plain.model.d.mts} +18 -18
- package/dist/models/{schema.typed.model.d.mts → schematic.typed.model.d.mts} +8 -8
- package/dist/models/transform.model.d.mts +6 -6
- package/dist/models/validation.model.d.mts +2 -2
- package/dist/{schematic.d.mts → schema.d.mts} +20 -20
- package/dist/{schematic.mjs → schema.mjs} +13 -13
- package/dist/validator/object.validator.mjs +63 -41
- package/dist/validator/schema.validator.d.mts +7 -0
- package/dist/validator/{schematic.validator.mjs → schema.validator.mjs} +5 -5
- package/package.json +1 -1
- package/src/constants.ts +3 -3
- package/src/helpers/misc.helper.ts +5 -5
- package/src/index.ts +4 -4
- package/src/models/infer.model.ts +26 -28
- package/src/models/misc.model.ts +3 -3
- package/src/models/{schema.plain.model.ts → schematic.plain.model.ts} +20 -20
- package/src/models/{schema.typed.model.ts → schematic.typed.model.ts} +8 -8
- package/src/models/transform.model.ts +6 -6
- package/src/models/validation.model.ts +2 -2
- package/src/{schematic.ts → schema.ts} +29 -27
- package/src/validator/object.validator.ts +123 -63
- package/src/validator/{schematic.validator.ts → schema.validator.ts} +3 -3
- package/dist/validator/schematic.validator.d.mts +0 -7
- /package/dist/models/{schema.plain.model.mjs → schematic.plain.model.mjs} +0 -0
- /package/dist/models/{schema.typed.model.mjs → schematic.typed.model.mjs} +0 -0
|
@@ -23,7 +23,7 @@ import {
|
|
|
23
23
|
getSchematicPropertyTypeMessage,
|
|
24
24
|
getUnknownKeysMessage,
|
|
25
25
|
} from '../helpers/message.helper';
|
|
26
|
-
import {getParameters,
|
|
26
|
+
import {getParameters, isSchema} from '../helpers/misc.helper';
|
|
27
27
|
import type {ValueName} from '../models/misc.model';
|
|
28
28
|
import {
|
|
29
29
|
type NamedValidatorHandlers,
|
|
@@ -34,13 +34,33 @@ import {
|
|
|
34
34
|
type Validator,
|
|
35
35
|
type ValidatorDefaults,
|
|
36
36
|
type ValidatorItem,
|
|
37
|
+
type ValidatorParameters,
|
|
37
38
|
type ValidatorType,
|
|
38
39
|
} from '../models/validation.model';
|
|
39
40
|
import {getBaseValidator} from './base.validator';
|
|
40
41
|
import {getFunctionValidator} from './function.validator';
|
|
41
42
|
import {getNamedHandlers} from './named.handler';
|
|
42
43
|
import {getNamedValidator} from './named.validator';
|
|
43
|
-
import {
|
|
44
|
+
import {getSchemaValidator} from './schema.validator';
|
|
45
|
+
|
|
46
|
+
type ReportParameters<Callback extends (...args: any[]) => string> = {
|
|
47
|
+
extract?: boolean;
|
|
48
|
+
information?: ReportParametersInformation;
|
|
49
|
+
key: ValidationInformationKey;
|
|
50
|
+
message: ReportParametersMessage<Callback>;
|
|
51
|
+
original: ValidatorParameters;
|
|
52
|
+
value: unknown;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
type ReportParametersMessage<Callback extends (...args: any[]) => string> = {
|
|
56
|
+
arguments: Parameters<Callback>;
|
|
57
|
+
callback: Callback;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
type ReportParametersInformation = {
|
|
61
|
+
all: ValidationInformation[];
|
|
62
|
+
existing?: ValidationInformation[];
|
|
63
|
+
};
|
|
44
64
|
|
|
45
65
|
function getDefaults(
|
|
46
66
|
obj: PlainObject,
|
|
@@ -106,12 +126,12 @@ export function getObjectValidator(
|
|
|
106
126
|
const key = keys[keyIndex];
|
|
107
127
|
const value = original[key];
|
|
108
128
|
|
|
129
|
+
const prefixedKey = origin == null ? key : join([origin.full, key], '.');
|
|
130
|
+
|
|
109
131
|
if (value == null) {
|
|
110
|
-
throw new SchematicError(getSchematicPropertyNullableMessage(
|
|
132
|
+
throw new SchematicError(getSchematicPropertyNullableMessage(prefixedKey));
|
|
111
133
|
}
|
|
112
134
|
|
|
113
|
-
const prefixedKey = origin == null ? key : join([origin.full, key], '.');
|
|
114
|
-
|
|
115
135
|
const fullKey: ValidationInformationKey = {
|
|
116
136
|
full: prefixedKey,
|
|
117
137
|
short: key,
|
|
@@ -161,8 +181,8 @@ export function getObjectValidator(
|
|
|
161
181
|
validator = getObjectValidator(type, fullKey, typed);
|
|
162
182
|
break;
|
|
163
183
|
|
|
164
|
-
case
|
|
165
|
-
validator =
|
|
184
|
+
case isSchema(type):
|
|
185
|
+
validator = getSchemaValidator(type);
|
|
166
186
|
break;
|
|
167
187
|
|
|
168
188
|
case TYPE_ALL.has(type as ValueName):
|
|
@@ -203,26 +223,23 @@ export function getObjectValidator(
|
|
|
203
223
|
|
|
204
224
|
return (input, parameters, get) => {
|
|
205
225
|
if (!isPlainObject(input)) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
parameters.information?.push(information);
|
|
224
|
-
|
|
225
|
-
return [information];
|
|
226
|
+
return origin == null
|
|
227
|
+
? report(
|
|
228
|
+
{
|
|
229
|
+
key: {
|
|
230
|
+
full: '',
|
|
231
|
+
short: '',
|
|
232
|
+
},
|
|
233
|
+
message: {
|
|
234
|
+
arguments: [input],
|
|
235
|
+
callback: getInputTypeMessage,
|
|
236
|
+
},
|
|
237
|
+
original: parameters,
|
|
238
|
+
value: input,
|
|
239
|
+
},
|
|
240
|
+
true,
|
|
241
|
+
)
|
|
242
|
+
: [];
|
|
226
243
|
}
|
|
227
244
|
|
|
228
245
|
if (parameters.strict) {
|
|
@@ -249,6 +266,8 @@ export function getObjectValidator(
|
|
|
249
266
|
}
|
|
250
267
|
}
|
|
251
268
|
|
|
269
|
+
const getAndClone = get && parameters.clone;
|
|
270
|
+
|
|
252
271
|
const allInformation: ValidationInformation[] = [];
|
|
253
272
|
const output: PlainObject = {};
|
|
254
273
|
|
|
@@ -260,7 +279,13 @@ export function getObjectValidator(
|
|
|
260
279
|
if (value === undefined) {
|
|
261
280
|
if (required) {
|
|
262
281
|
if (get && defaults != null) {
|
|
263
|
-
|
|
282
|
+
const defaultValue = clone(defaults.value);
|
|
283
|
+
|
|
284
|
+
if (parameters.clone) {
|
|
285
|
+
output[key.short] = defaultValue;
|
|
286
|
+
} else {
|
|
287
|
+
input[key.short] = defaultValue;
|
|
288
|
+
}
|
|
264
289
|
|
|
265
290
|
continue;
|
|
266
291
|
}
|
|
@@ -269,25 +294,24 @@ export function getObjectValidator(
|
|
|
269
294
|
return [];
|
|
270
295
|
}
|
|
271
296
|
|
|
272
|
-
const
|
|
297
|
+
const reported = report({
|
|
273
298
|
key,
|
|
274
299
|
value,
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
300
|
+
information: {
|
|
301
|
+
all: allInformation,
|
|
302
|
+
},
|
|
303
|
+
message: {
|
|
304
|
+
arguments: [key.full, types],
|
|
305
|
+
callback: getInputPropertyMissingMessage,
|
|
306
|
+
},
|
|
307
|
+
original: parameters,
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
if (reported == null) {
|
|
287
311
|
continue;
|
|
288
312
|
}
|
|
289
313
|
|
|
290
|
-
return
|
|
314
|
+
return reported;
|
|
291
315
|
}
|
|
292
316
|
|
|
293
317
|
continue;
|
|
@@ -302,8 +326,8 @@ export function getObjectValidator(
|
|
|
302
326
|
parameters.output = previousOutput;
|
|
303
327
|
|
|
304
328
|
if (result === true) {
|
|
305
|
-
if (
|
|
306
|
-
output[key.short] =
|
|
329
|
+
if (getAndClone && !isPlainObject(value)) {
|
|
330
|
+
output[key.short] = clone(value);
|
|
307
331
|
}
|
|
308
332
|
|
|
309
333
|
continue;
|
|
@@ -313,31 +337,29 @@ export function getObjectValidator(
|
|
|
313
337
|
return [];
|
|
314
338
|
}
|
|
315
339
|
|
|
316
|
-
const
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
if (parameters.reporting.all) {
|
|
332
|
-
allInformation.push(...information);
|
|
340
|
+
const reported = report({
|
|
341
|
+
key,
|
|
342
|
+
value,
|
|
343
|
+
extract: false,
|
|
344
|
+
information: {
|
|
345
|
+
all: allInformation,
|
|
346
|
+
existing: typeof result !== 'boolean' && result.length > 0 ? result : undefined,
|
|
347
|
+
},
|
|
348
|
+
message: {
|
|
349
|
+
arguments: [key.full, types, value],
|
|
350
|
+
callback: getInputPropertyTypeMessage,
|
|
351
|
+
},
|
|
352
|
+
original: parameters,
|
|
353
|
+
});
|
|
333
354
|
|
|
355
|
+
if (reported == null) {
|
|
334
356
|
continue;
|
|
335
357
|
}
|
|
336
358
|
|
|
337
|
-
return
|
|
359
|
+
return reported;
|
|
338
360
|
}
|
|
339
361
|
|
|
340
|
-
if (
|
|
362
|
+
if (getAndClone) {
|
|
341
363
|
if (origin == null) {
|
|
342
364
|
parameters.output = output;
|
|
343
365
|
} else {
|
|
@@ -364,3 +386,41 @@ function getRequired(obj: PlainObject, key: string, allowed: boolean): boolean |
|
|
|
364
386
|
|
|
365
387
|
return obj[PROPERTY_REQUIRED];
|
|
366
388
|
}
|
|
389
|
+
|
|
390
|
+
function report<Callback extends (...args: any[]) => string>(
|
|
391
|
+
parameters: ReportParameters<Callback>,
|
|
392
|
+
getReports: true,
|
|
393
|
+
): ValidationInformation[];
|
|
394
|
+
|
|
395
|
+
function report<Callback extends (...args: any[]) => string>(
|
|
396
|
+
parameters: ReportParameters<Callback>,
|
|
397
|
+
): ValidationInformation[] | undefined;
|
|
398
|
+
|
|
399
|
+
function report<Callback extends (...args: any[]) => string>(
|
|
400
|
+
parameters: ReportParameters<Callback>,
|
|
401
|
+
getReports?: boolean,
|
|
402
|
+
): ValidationInformation[] | undefined {
|
|
403
|
+
const {information, message, original} = parameters;
|
|
404
|
+
|
|
405
|
+
const reported: ValidationInformation[] = information?.existing ?? [
|
|
406
|
+
{
|
|
407
|
+
key: parameters.key,
|
|
408
|
+
value: parameters.value,
|
|
409
|
+
message: message.callback(...message.arguments),
|
|
410
|
+
},
|
|
411
|
+
];
|
|
412
|
+
|
|
413
|
+
if (original.reporting.throw) {
|
|
414
|
+
throw new ValidationError(reported);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
information?.all.push(...reported);
|
|
418
|
+
|
|
419
|
+
if (parameters.extract ?? true) {
|
|
420
|
+
original.information?.push(...reported);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
if ((getReports ?? false) || !original.reporting.all) {
|
|
424
|
+
return reported;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
2
|
import type {Validator} from '../models/validation.model';
|
|
3
|
-
import {
|
|
3
|
+
import {Schema, schemaValidators} from '../schema';
|
|
4
4
|
|
|
5
|
-
export function
|
|
6
|
-
const validator =
|
|
5
|
+
export function getSchemaValidator(schematic: Schema<unknown>): Validator {
|
|
6
|
+
const validator = schemaValidators.get(schematic)!;
|
|
7
7
|
|
|
8
8
|
return (input, parameters, get) => {
|
|
9
9
|
let result: ReturnType<Validator>;
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { Validator } from "../models/validation.model.mjs";
|
|
2
|
-
import { Schematic } from "../schematic.mjs";
|
|
3
|
-
|
|
4
|
-
//#region src/validator/schematic.validator.d.ts
|
|
5
|
-
declare function getSchematicValidator(schematic: Schematic<unknown>): Validator;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { getSchematicValidator };
|
|
File without changes
|
|
File without changes
|