@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.
Files changed (33) hide show
  1. package/dist/constants.d.mts +4 -4
  2. package/dist/constants.mjs +4 -4
  3. package/dist/helpers/misc.helper.d.mts +3 -3
  4. package/dist/helpers/misc.helper.mjs +3 -3
  5. package/dist/index.d.mts +64 -64
  6. package/dist/index.mjs +78 -56
  7. package/dist/models/infer.model.d.mts +21 -21
  8. package/dist/models/misc.model.d.mts +3 -3
  9. package/dist/models/{schema.plain.model.d.mts → schematic.plain.model.d.mts} +18 -18
  10. package/dist/models/{schema.typed.model.d.mts → schematic.typed.model.d.mts} +8 -8
  11. package/dist/models/transform.model.d.mts +6 -6
  12. package/dist/models/validation.model.d.mts +2 -2
  13. package/dist/{schematic.d.mts → schema.d.mts} +20 -20
  14. package/dist/{schematic.mjs → schema.mjs} +13 -13
  15. package/dist/validator/object.validator.mjs +63 -41
  16. package/dist/validator/schema.validator.d.mts +7 -0
  17. package/dist/validator/{schematic.validator.mjs → schema.validator.mjs} +5 -5
  18. package/package.json +1 -1
  19. package/src/constants.ts +3 -3
  20. package/src/helpers/misc.helper.ts +5 -5
  21. package/src/index.ts +4 -4
  22. package/src/models/infer.model.ts +26 -28
  23. package/src/models/misc.model.ts +3 -3
  24. package/src/models/{schema.plain.model.ts → schematic.plain.model.ts} +20 -20
  25. package/src/models/{schema.typed.model.ts → schematic.typed.model.ts} +8 -8
  26. package/src/models/transform.model.ts +6 -6
  27. package/src/models/validation.model.ts +2 -2
  28. package/src/{schematic.ts → schema.ts} +29 -27
  29. package/src/validator/object.validator.ts +123 -63
  30. package/src/validator/{schematic.validator.ts → schema.validator.ts} +3 -3
  31. package/dist/validator/schematic.validator.d.mts +0 -7
  32. /package/dist/models/{schema.plain.model.mjs → schematic.plain.model.mjs} +0 -0
  33. /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, isSchematic} from '../helpers/misc.helper';
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 {getSchematicValidator} from './schematic.validator';
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(join([origin?.full, key], '.')));
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 isSchematic(type):
165
- validator = getSchematicValidator(type);
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
- 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
+ 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
- output[key.short] = clone(defaults.value);
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 information: ValidationInformation = {
297
+ const reported = report({
273
298
  key,
274
299
  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
-
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 [information];
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 (get && !isPlainObject(value)) {
306
- output[key.short] = parameters.clone ? clone(value) : value;
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 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);
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 information;
359
+ return reported;
338
360
  }
339
361
 
340
- if (get) {
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 {Schematic, schematicValidator} from '../schematic';
3
+ import {Schema, schemaValidators} from '../schema';
4
4
 
5
- export function getSchematicValidator(schematic: Schematic<unknown>): Validator {
6
- const validator = schematicValidator.get(schematic)!;
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 };