@oscarpalmer/jhunal 0.23.0 → 0.25.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 (40) hide show
  1. package/dist/constants.d.mts +8 -5
  2. package/dist/constants.mjs +20 -23
  3. package/dist/helpers/message.helper.d.mts +11 -5
  4. package/dist/helpers/message.helper.mjs +31 -9
  5. package/dist/helpers/misc.helper.d.mts +4 -4
  6. package/dist/helpers/misc.helper.mjs +4 -4
  7. package/dist/index.d.mts +66 -78
  8. package/dist/index.mjs +100 -62
  9. package/dist/models/infer.model.d.mts +21 -21
  10. package/dist/models/misc.model.d.mts +3 -3
  11. package/dist/models/{schema.plain.model.d.mts → schematic.plain.model.d.mts} +20 -18
  12. package/dist/models/{schema.typed.model.d.mts → schematic.typed.model.d.mts} +10 -24
  13. package/dist/models/transform.model.d.mts +6 -6
  14. package/dist/models/validation.model.d.mts +13 -3
  15. package/dist/{schematic.d.mts → schema.d.mts} +18 -18
  16. package/dist/{schematic.mjs → schema.mjs} +12 -12
  17. package/dist/validator/named.handler.d.mts +1 -1
  18. package/dist/validator/named.handler.mjs +3 -2
  19. package/dist/validator/named.validator.mjs +2 -3
  20. package/dist/validator/object.validator.mjs +40 -22
  21. package/dist/validator/schematic.validator.d.mts +3 -3
  22. package/dist/validator/schematic.validator.mjs +4 -4
  23. package/package.json +1 -1
  24. package/src/constants.ts +24 -28
  25. package/src/helpers/message.helper.ts +74 -9
  26. package/src/helpers/misc.helper.ts +9 -10
  27. package/src/index.ts +4 -4
  28. package/src/models/infer.model.ts +26 -28
  29. package/src/models/misc.model.ts +3 -3
  30. package/src/models/{schema.plain.model.ts → schematic.plain.model.ts} +22 -20
  31. package/src/models/{schema.typed.model.ts → schematic.typed.model.ts} +10 -28
  32. package/src/models/transform.model.ts +6 -6
  33. package/src/models/validation.model.ts +14 -2
  34. package/src/{schematic.ts → schema.ts} +23 -23
  35. package/src/validator/named.handler.ts +16 -1
  36. package/src/validator/named.validator.ts +3 -4
  37. package/src/validator/object.validator.ts +81 -55
  38. package/src/validator/schematic.validator.ts +3 -3
  39. /package/dist/models/{schema.plain.model.mjs → schematic.plain.model.mjs} +0 -0
  40. /package/dist/models/{schema.typed.model.mjs → schematic.typed.model.mjs} +0 -0
@@ -3,25 +3,27 @@ import type {PlainObject} from '@oscarpalmer/atoms/models';
3
3
  import {join} from '@oscarpalmer/atoms/string';
4
4
  import {clone} from '@oscarpalmer/atoms/value/clone';
5
5
  import {
6
+ PROPERTY_DEFAULT,
6
7
  PROPERTY_REQUIRED,
7
8
  PROPERTY_TYPE,
8
9
  PROPERTY_VALIDATORS,
9
10
  SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY,
10
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED,
11
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE,
12
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED,
13
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE,
14
- TEMPLATE_PATTERN,
15
11
  TYPE_ALL,
16
12
  TYPE_UNDEFINED,
17
13
  } from '../constants';
18
14
  import {
19
- getInvalidInputMessage,
20
- getInvalidMissingMessage,
21
- getInvalidTypeMessage,
15
+ getDefaultRequiredMessage,
16
+ getDefaultTypeMessage,
17
+ getDisallowedMessage,
18
+ getInputPropertyMissingMessage,
19
+ getInputPropertyTypeMessage,
20
+ getInputTypeMessage,
21
+ getRequiredMessage,
22
+ getSchematicPropertyNullableMessage,
23
+ getSchematicPropertyTypeMessage,
22
24
  getUnknownKeysMessage,
23
25
  } from '../helpers/message.helper';
24
- import {isSchematic} from '../helpers/misc.helper';
26
+ import {getParameters, isSchema} from '../helpers/misc.helper';
25
27
  import type {ValueName} from '../models/misc.model';
26
28
  import {
27
29
  type NamedValidatorHandlers,
@@ -30,15 +32,39 @@ import {
30
32
  type ValidationInformation,
31
33
  type ValidationInformationKey,
32
34
  type Validator,
35
+ type ValidatorDefaults,
36
+ type ValidatorItem,
33
37
  type ValidatorType,
34
38
  } from '../models/validation.model';
35
39
  import {getBaseValidator} from './base.validator';
36
40
  import {getFunctionValidator} from './function.validator';
37
41
  import {getNamedHandlers} from './named.handler';
38
42
  import {getNamedValidator} from './named.validator';
39
- import {getSchematicValidator} from './schematic.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
+ }
40
62
 
41
63
  function getDisallowedProperty(obj: PlainObject): string | undefined {
64
+ if (PROPERTY_DEFAULT in obj) {
65
+ return PROPERTY_DEFAULT;
66
+ }
67
+
42
68
  if (PROPERTY_REQUIRED in obj) {
43
69
  return PROPERTY_REQUIRED;
44
70
  }
@@ -68,35 +94,20 @@ export function getObjectValidator(
68
94
  const property = getDisallowedProperty(original);
69
95
 
70
96
  if (property != null) {
71
- throw new SchematicError(
72
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED.replace(
73
- TEMPLATE_PATTERN,
74
- origin!.full,
75
- ).replace(TEMPLATE_PATTERN, property),
76
- );
97
+ throw new SchematicError(getDisallowedMessage(origin!.full, property));
77
98
  }
78
99
  }
79
100
 
80
101
  const set = new Set<string>();
81
102
 
82
- const items: {
83
- key: ValidationInformationKey;
84
- required: boolean;
85
- types: ValidatorType[];
86
- validator: Validator;
87
- }[] = [];
103
+ const items: ValidatorItem[] = [];
88
104
 
89
105
  for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
90
106
  const key = keys[keyIndex];
91
107
  const value = original[key];
92
108
 
93
109
  if (value == null) {
94
- throw new SchematicError(
95
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE.replace(
96
- TEMPLATE_PATTERN,
97
- join([origin?.full, key], '.'),
98
- ),
99
- );
110
+ throw new SchematicError(getSchematicPropertyNullableMessage(join([origin?.full, key], '.')));
100
111
  }
101
112
 
102
113
  const prefixedKey = origin == null ? key : join([origin.full, key], '.');
@@ -109,6 +120,9 @@ export function getObjectValidator(
109
120
  let handlers: NamedValidatorHandlers = {};
110
121
  let required = true;
111
122
  let typed = false;
123
+
124
+ let defaults: ValidatorDefaults | undefined;
125
+
112
126
  let types: ValidatorType[];
113
127
 
114
128
  const validators: Validator[] = [];
@@ -118,8 +132,9 @@ export function getObjectValidator(
118
132
 
119
133
  const type = typed ? value[PROPERTY_TYPE] : value;
120
134
 
121
- handlers = getNamedHandlers(value[PROPERTY_VALIDATORS], prefixedKey);
122
- required = getRequired(key, value) ?? required;
135
+ defaults = getDefaults(value, prefixedKey, typed);
136
+ handlers = getNamedHandlers(value[PROPERTY_VALIDATORS], prefixedKey, typed);
137
+ required = getRequired(value, prefixedKey, typed) ?? required;
123
138
 
124
139
  types = Array.isArray(type) ? type : [type];
125
140
  } else {
@@ -127,12 +142,7 @@ export function getObjectValidator(
127
142
  }
128
143
 
129
144
  if (types.length === 0) {
130
- throw new SchematicError(
131
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE.replace(
132
- TEMPLATE_PATTERN,
133
- prefixedKey,
134
- ).replace(TEMPLATE_PATTERN, String(value)),
135
- );
145
+ throw new SchematicError(getSchematicPropertyTypeMessage(prefixedKey));
136
146
  }
137
147
 
138
148
  const typesLength = types.length;
@@ -151,8 +161,8 @@ export function getObjectValidator(
151
161
  validator = getObjectValidator(type, fullKey, typed);
152
162
  break;
153
163
 
154
- case isSchematic(type):
155
- validator = getSchematicValidator(type);
164
+ case isSchema(type):
165
+ validator = getSchemaValidator(type);
156
166
  break;
157
167
 
158
168
  case TYPE_ALL.has(type as ValueName):
@@ -160,25 +170,33 @@ export function getObjectValidator(
160
170
  break;
161
171
 
162
172
  default:
163
- throw new SchematicError(
164
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE.replace(
165
- TEMPLATE_PATTERN,
166
- prefixedKey,
167
- ).replace(TEMPLATE_PATTERN, String(type)),
168
- );
173
+ throw new SchematicError(getSchematicPropertyTypeMessage(prefixedKey));
169
174
  }
170
175
 
171
176
  validators.push(validator);
172
177
  }
173
178
 
174
- set.add(key);
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
+ }
175
190
 
176
191
  items.push({
192
+ defaults,
193
+ required,
177
194
  types,
195
+ validator,
178
196
  key: fullKey,
179
- required: required && !types.includes(TYPE_UNDEFINED),
180
- validator: getBaseValidator(validators),
181
197
  });
198
+
199
+ set.add(key);
182
200
  }
183
201
 
184
202
  const validatorsLength = items.length;
@@ -195,7 +213,7 @@ export function getObjectValidator(
195
213
  short: '',
196
214
  },
197
215
  value: input,
198
- message: getInvalidInputMessage(input),
216
+ message: getInputTypeMessage(input),
199
217
  };
200
218
 
201
219
  if (parameters.reporting.throw) {
@@ -235,12 +253,18 @@ export function getObjectValidator(
235
253
  const output: PlainObject = {};
236
254
 
237
255
  for (let validatorIndex = 0; validatorIndex < validatorsLength; validatorIndex += 1) {
238
- const {key, required, types, validator} = items[validatorIndex];
256
+ const {defaults, key, required, types, validator} = items[validatorIndex];
239
257
 
240
258
  const value = (input as PlainObject)[key.short];
241
259
 
242
260
  if (value === undefined) {
243
261
  if (required) {
262
+ if (get && defaults != null) {
263
+ output[key.short] = clone(defaults.value);
264
+
265
+ continue;
266
+ }
267
+
244
268
  if (parameters.reporting.none) {
245
269
  return [];
246
270
  }
@@ -248,7 +272,7 @@ export function getObjectValidator(
248
272
  const information: ValidationInformation = {
249
273
  key,
250
274
  value,
251
- message: getInvalidMissingMessage(key.full, types),
275
+ message: getInputPropertyMissingMessage(key.full, types),
252
276
  };
253
277
 
254
278
  if (parameters.reporting.throw) {
@@ -296,7 +320,7 @@ export function getObjectValidator(
296
320
  {
297
321
  key,
298
322
  value,
299
- message: getInvalidTypeMessage(key.full, types, value),
323
+ message: getInputPropertyTypeMessage(key.full, types, value),
300
324
  },
301
325
  ];
302
326
 
@@ -325,15 +349,17 @@ export function getObjectValidator(
325
349
  };
326
350
  }
327
351
 
328
- function getRequired(key: string, obj: PlainObject): boolean | undefined {
352
+ function getRequired(obj: PlainObject, key: string, allowed: boolean): boolean | undefined {
329
353
  if (!(PROPERTY_REQUIRED in obj)) {
330
354
  return;
331
355
  }
332
356
 
357
+ if (!allowed) {
358
+ throw new SchematicError(getDisallowedMessage(key, PROPERTY_REQUIRED));
359
+ }
360
+
333
361
  if (typeof obj[PROPERTY_REQUIRED] !== 'boolean') {
334
- throw new SchematicError(
335
- SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED.replace(TEMPLATE_PATTERN, key),
336
- );
362
+ throw new SchematicError(getRequiredMessage(key));
337
363
  }
338
364
 
339
365
  return obj[PROPERTY_REQUIRED];
@@ -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>;