@oscarpalmer/jhunal 0.22.0 → 0.24.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 +11 -4
- package/dist/constants.mjs +27 -8
- package/dist/helpers/message.helper.d.mts +17 -0
- package/dist/helpers/message.helper.mjs +92 -0
- package/dist/helpers/misc.helper.d.mts +22 -0
- package/dist/helpers/misc.helper.mjs +56 -0
- package/dist/index.d.mts +309 -292
- package/dist/index.mjs +237 -166
- package/dist/models/schema.plain.model.d.mts +2 -0
- package/dist/models/schema.typed.model.d.mts +3 -17
- package/dist/models/validation.model.d.mts +28 -7
- package/dist/schematic.d.mts +25 -7
- package/dist/schematic.mjs +6 -4
- package/dist/validator/base.validator.d.mts +6 -0
- package/dist/validator/base.validator.mjs +19 -0
- package/dist/validator/function.validator.d.mts +6 -0
- package/dist/validator/function.validator.mjs +9 -0
- package/dist/validator/named.handler.d.mts +6 -0
- package/dist/validator/named.handler.mjs +23 -0
- package/dist/validator/named.validator.d.mts +7 -0
- package/dist/validator/named.validator.mjs +38 -0
- package/dist/validator/object.validator.d.mts +7 -0
- package/dist/validator/object.validator.mjs +185 -0
- package/dist/validator/schematic.validator.d.mts +7 -0
- package/dist/validator/schematic.validator.mjs +16 -0
- package/package.json +1 -1
- package/src/constants.ts +34 -6
- package/src/helpers/message.helper.ts +217 -0
- package/src/helpers/misc.helper.ts +92 -0
- package/src/index.ts +3 -3
- package/src/models/schema.plain.model.ts +2 -0
- package/src/models/schema.typed.model.ts +4 -22
- package/src/models/validation.model.ts +31 -6
- package/src/schematic.ts +43 -16
- package/src/validator/base.validator.ts +31 -0
- package/src/validator/function.validator.ts +9 -0
- package/src/validator/named.handler.ts +65 -0
- package/src/validator/named.validator.ts +61 -0
- package/src/validator/object.validator.ts +366 -0
- package/src/validator/schematic.validator.ts +25 -0
- package/dist/helpers.d.mts +0 -28
- package/dist/helpers.mjs +0 -120
- package/dist/validation.d.mts +0 -7
- package/dist/validation.mjs +0 -245
- package/src/helpers.ts +0 -249
- package/src/validation.ts +0 -498
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import {getInputPropertyValidatorMessage} from '../helpers/message.helper';
|
|
2
|
+
import type {ValueName} from '../models/misc.model';
|
|
3
|
+
import type {
|
|
4
|
+
NamedValidatorHandlers,
|
|
5
|
+
NamedValidators,
|
|
6
|
+
ValidationInformation,
|
|
7
|
+
ValidationInformationKey,
|
|
8
|
+
Validator,
|
|
9
|
+
} from '../models/validation.model';
|
|
10
|
+
|
|
11
|
+
export function getNamedValidator(
|
|
12
|
+
key: ValidationInformationKey,
|
|
13
|
+
name: ValueName,
|
|
14
|
+
handlers: NamedValidatorHandlers,
|
|
15
|
+
): Validator {
|
|
16
|
+
const validator = namedValidators[name];
|
|
17
|
+
|
|
18
|
+
const named = handlers[name] ?? [];
|
|
19
|
+
const {length} = named;
|
|
20
|
+
|
|
21
|
+
return (input, parameters) => {
|
|
22
|
+
if (!validator(input)) {
|
|
23
|
+
return [];
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (let index = 0; index < length; index += 1) {
|
|
27
|
+
const handler = named[index];
|
|
28
|
+
|
|
29
|
+
if (handler(input) === true) {
|
|
30
|
+
continue;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const information: ValidationInformation = {
|
|
34
|
+
key,
|
|
35
|
+
validator,
|
|
36
|
+
message: getInputPropertyValidatorMessage(key.full, name, index, length),
|
|
37
|
+
value: input,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
parameters.information?.push(information);
|
|
41
|
+
|
|
42
|
+
return parameters.reporting.none ? [] : [information];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return true;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const namedValidators: NamedValidators = {
|
|
50
|
+
array: Array.isArray,
|
|
51
|
+
bigint: value => typeof value === 'bigint',
|
|
52
|
+
boolean: value => typeof value === 'boolean',
|
|
53
|
+
date: value => value instanceof Date,
|
|
54
|
+
function: value => typeof value === 'function',
|
|
55
|
+
null: value => value === null,
|
|
56
|
+
number: value => typeof value === 'number',
|
|
57
|
+
object: value => typeof value === 'object' && value !== null,
|
|
58
|
+
string: value => typeof value === 'string',
|
|
59
|
+
symbol: value => typeof value === 'symbol',
|
|
60
|
+
undefined: value => value === undefined,
|
|
61
|
+
};
|
|
@@ -0,0 +1,366 @@
|
|
|
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, isSchematic} 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 {getSchematicValidator} 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 isSchematic(type):
|
|
165
|
+
validator = getSchematicValidator(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
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import {isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
|
+
import type {Validator} from '../models/validation.model';
|
|
3
|
+
import {Schematic, schematicValidator} from '../schematic';
|
|
4
|
+
|
|
5
|
+
export function getSchematicValidator(schematic: Schematic<unknown>): Validator {
|
|
6
|
+
const validator = schematicValidator.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
|
+
}
|
package/dist/helpers.d.mts
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ReportingInformation, ValidatorParameters, ValidatorType } from "./models/validation.model.mjs";
|
|
2
|
-
import { Schematic } from "./schematic.mjs";
|
|
3
|
-
import { ValueName } from "./models/misc.model.mjs";
|
|
4
|
-
import { Constructor } from "@oscarpalmer/atoms/models";
|
|
5
|
-
|
|
6
|
-
//#region src/helpers.d.ts
|
|
7
|
-
declare function getInvalidInputMessage(actual: unknown): string;
|
|
8
|
-
declare function getInvalidMissingMessage(key: string, types: ValidatorType[]): string;
|
|
9
|
-
declare function getInvalidTypeMessage(key: string, types: ValidatorType[], actual: unknown): string;
|
|
10
|
-
declare function getInvalidValidatorMessage(key: string, type: ValueName, index: number, length: number): string;
|
|
11
|
-
declare function getParameters(input?: unknown): ValidatorParameters;
|
|
12
|
-
declare function getReporting(value: unknown): ReportingInformation;
|
|
13
|
-
declare function getUnknownKeysMessage(keys: string[]): string;
|
|
14
|
-
/**
|
|
15
|
-
* Creates a validator function for a given constructor
|
|
16
|
-
* @param constructor - Constructor to check against
|
|
17
|
-
* @throws Will throw a `TypeError` if the provided argument is not a valid constructor
|
|
18
|
-
* @returns Validator function that checks if a value is an instance of the constructor
|
|
19
|
-
*/
|
|
20
|
-
declare function instanceOf<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
|
|
21
|
-
/**
|
|
22
|
-
* Is the value a schematic?
|
|
23
|
-
* @param value Value to check
|
|
24
|
-
* @returns `true` if the value is a schematic, `false` otherwise
|
|
25
|
-
*/
|
|
26
|
-
declare function isSchematic(value: unknown): value is Schematic<never>;
|
|
27
|
-
//#endregion
|
|
28
|
-
export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getParameters, getReporting, getUnknownKeysMessage, instanceOf, isSchematic };
|
package/dist/helpers.mjs
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import { CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_SCHEMATIC, REPORTING_FIRST, REPORTING_NONE, REPORTING_THROW, REPORTING_TYPES, TYPE_ARRAY, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATION_MESSAGE_INVALID_INPUT, VALIDATION_MESSAGE_INVALID_REQUIRED, VALIDATION_MESSAGE_INVALID_TYPE, VALIDATION_MESSAGE_INVALID_VALUE, VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX, VALIDATION_MESSAGE_UNKNOWN_KEYS } from "./constants.mjs";
|
|
2
|
-
import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
|
-
//#region src/helpers.ts
|
|
4
|
-
function getInvalidInputMessage(actual) {
|
|
5
|
-
return VALIDATION_MESSAGE_INVALID_INPUT.replace("<>", getValueType(actual));
|
|
6
|
-
}
|
|
7
|
-
function getInvalidMissingMessage(key, types) {
|
|
8
|
-
let message = VALIDATION_MESSAGE_INVALID_REQUIRED.replace("<>", renderTypes(types));
|
|
9
|
-
message = message.replace("<>", key);
|
|
10
|
-
return message;
|
|
11
|
-
}
|
|
12
|
-
function getInvalidTypeMessage(key, types, actual) {
|
|
13
|
-
let message = VALIDATION_MESSAGE_INVALID_TYPE.replace("<>", renderTypes(types));
|
|
14
|
-
message = message.replace("<>", key);
|
|
15
|
-
message = message.replace("<>", getValueType(actual));
|
|
16
|
-
return message;
|
|
17
|
-
}
|
|
18
|
-
function getInvalidValidatorMessage(key, type, index, length) {
|
|
19
|
-
let message = VALIDATION_MESSAGE_INVALID_VALUE.replace("<>", key);
|
|
20
|
-
message = message.replace("<>", type);
|
|
21
|
-
if (length > 1) message += VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX.replace("<>", String(index));
|
|
22
|
-
return message;
|
|
23
|
-
}
|
|
24
|
-
function getParameters(input) {
|
|
25
|
-
if (typeof input === "boolean") return {
|
|
26
|
-
output: {},
|
|
27
|
-
reporting: getReporting(REPORTING_NONE),
|
|
28
|
-
strict: input
|
|
29
|
-
};
|
|
30
|
-
if (REPORTING_TYPES.has(input)) return {
|
|
31
|
-
output: {},
|
|
32
|
-
reporting: getReporting(input),
|
|
33
|
-
strict: false
|
|
34
|
-
};
|
|
35
|
-
const options = isPlainObject(input) ? input : {};
|
|
36
|
-
return {
|
|
37
|
-
output: {},
|
|
38
|
-
reporting: getReporting(options.errors),
|
|
39
|
-
strict: typeof options.strict === "boolean" ? options.strict : false
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
function getPropertyType(original) {
|
|
43
|
-
if (typeof original === "function") return "a validated value";
|
|
44
|
-
if (Array.isArray(original)) return `'array'`;
|
|
45
|
-
if (isPlainObject(original)) return `'${TYPE_OBJECT}'`;
|
|
46
|
-
if (isSchematic(original)) return `a ${NAME_SCHEMATIC}`;
|
|
47
|
-
return `'${String(original)}'`;
|
|
48
|
-
}
|
|
49
|
-
function getReporting(value) {
|
|
50
|
-
const type = REPORTING_TYPES.has(value) ? value : REPORTING_NONE;
|
|
51
|
-
return {
|
|
52
|
-
type,
|
|
53
|
-
["all"]: type === "all",
|
|
54
|
-
[REPORTING_FIRST]: type === REPORTING_FIRST,
|
|
55
|
-
[REPORTING_NONE]: type === REPORTING_NONE,
|
|
56
|
-
[REPORTING_THROW]: type === REPORTING_THROW
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function getUnknownKeysMessage(keys) {
|
|
60
|
-
return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace("<>", renderKeys(keys));
|
|
61
|
-
}
|
|
62
|
-
function getValueType(value) {
|
|
63
|
-
const valueType = typeof value;
|
|
64
|
-
switch (true) {
|
|
65
|
-
case value === null: return `'${TYPE_NULL}'`;
|
|
66
|
-
case value === void 0: return `'${TYPE_UNDEFINED}'`;
|
|
67
|
-
case valueType !== TYPE_OBJECT: return `'${valueType}'`;
|
|
68
|
-
case Array.isArray(value): return `'${TYPE_ARRAY}'`;
|
|
69
|
-
case isPlainObject(value): return `'${TYPE_OBJECT}'`;
|
|
70
|
-
case isSchematic(value): return `a ${NAME_SCHEMATIC}`;
|
|
71
|
-
default: return value.constructor.name;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* Creates a validator function for a given constructor
|
|
76
|
-
* @param constructor - Constructor to check against
|
|
77
|
-
* @throws Will throw a `TypeError` if the provided argument is not a valid constructor
|
|
78
|
-
* @returns Validator function that checks if a value is an instance of the constructor
|
|
79
|
-
*/
|
|
80
|
-
function instanceOf(constructor) {
|
|
81
|
-
if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
|
|
82
|
-
return (value) => {
|
|
83
|
-
return value instanceof constructor;
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* Is the value a schematic?
|
|
88
|
-
* @param value Value to check
|
|
89
|
-
* @returns `true` if the value is a schematic, `false` otherwise
|
|
90
|
-
*/
|
|
91
|
-
function isSchematic(value) {
|
|
92
|
-
return typeof value === "object" && value !== null && "$schematic" in value && value["$schematic"] === true;
|
|
93
|
-
}
|
|
94
|
-
function renderKeys(keys) {
|
|
95
|
-
return renderParts(keys.map((key) => `'${key}'`), CONJUNCTION_AND, CONJUNCTION_AND_COMMA);
|
|
96
|
-
}
|
|
97
|
-
function renderParts(parts, delimiterShort, delimiterLong) {
|
|
98
|
-
const { length } = parts;
|
|
99
|
-
if (length === 1) return parts[0];
|
|
100
|
-
let rendered = "";
|
|
101
|
-
for (let index = 0; index < length; index += 1) {
|
|
102
|
-
rendered += parts[index];
|
|
103
|
-
if (index < length - 2) rendered += ", ";
|
|
104
|
-
else if (index === length - 2) rendered += parts.length > 2 ? delimiterLong : delimiterShort;
|
|
105
|
-
}
|
|
106
|
-
return rendered;
|
|
107
|
-
}
|
|
108
|
-
function renderTypes(types) {
|
|
109
|
-
const unique = /* @__PURE__ */ new Set();
|
|
110
|
-
const parts = [];
|
|
111
|
-
for (let index = 0; index < types.length; index += 1) {
|
|
112
|
-
const rendered = getPropertyType(types[index]);
|
|
113
|
-
if (unique.has(rendered)) continue;
|
|
114
|
-
unique.add(rendered);
|
|
115
|
-
parts.push(rendered);
|
|
116
|
-
}
|
|
117
|
-
return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
|
|
118
|
-
}
|
|
119
|
-
//#endregion
|
|
120
|
-
export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getParameters, getReporting, getUnknownKeysMessage, instanceOf, isSchematic };
|
package/dist/validation.d.mts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { ValidationInformationKey, Validator } from "./models/validation.model.mjs";
|
|
2
|
-
import { PlainObject } from "@oscarpalmer/atoms";
|
|
3
|
-
|
|
4
|
-
//#region src/validation.d.ts
|
|
5
|
-
declare function getObjectValidator(original: PlainObject, origin?: ValidationInformationKey, fromType?: boolean): Validator;
|
|
6
|
-
//#endregion
|
|
7
|
-
export { getObjectValidator };
|