@oscarpalmer/jhunal 0.26.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 +170 -71
- package/dist/index.mjs +354 -215
- 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 +32 -32
- 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 +42 -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 -207
- package/dist/validator/schema.validator.d.mts +0 -7
- package/dist/validator/schema.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 -426
- package/src/validator/schema.validator.ts +0 -25
package/dist/constants.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReportingType } from "./models/validation.model.mjs";
|
|
2
|
-
import {
|
|
2
|
+
import { ValueType, Values } from "./models/misc.model.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/constants.d.ts
|
|
5
5
|
declare const COMMA = ", ";
|
|
@@ -12,20 +12,25 @@ declare const NAME_SCHEMA = "Schema";
|
|
|
12
12
|
declare const NAME_SCHEMA_PREFIXED = "a Schema";
|
|
13
13
|
declare const NAME_ERROR_SCHEMATIC = "SchematicError";
|
|
14
14
|
declare const NAME_ERROR_VALIDATION = "ValidationError";
|
|
15
|
+
declare const NAME_ERROR_VALIDATOR = "ValidatorError";
|
|
15
16
|
declare const PROPERTY_DEFAULT = "$default";
|
|
16
17
|
declare const PROPERTY_REQUIRED = "$required";
|
|
17
18
|
declare const PROPERTY_SCHEMA = "$schema";
|
|
18
19
|
declare const PROPERTY_TYPE = "$type";
|
|
20
|
+
declare const PROPERTY_VALIDATOR = "$validator";
|
|
19
21
|
declare const PROPERTY_VALIDATORS = "$validators";
|
|
20
22
|
declare const VALIDATION_MESSAGE_INVALID_INPUT = "Expected an object as input but received <>";
|
|
21
23
|
declare const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '<>'";
|
|
22
|
-
declare const
|
|
23
|
-
declare const
|
|
24
|
-
declare const
|
|
24
|
+
declare const VALIDATION_MESSAGE_INVALID_PROPERTY_TYPE = "Expected <> for '<>' but received <>";
|
|
25
|
+
declare const VALIDATION_MESSAGE_INVALID_PROPERTY_VALIDATOR = "Value does not satisfy validator for '<>' and type '<>'";
|
|
26
|
+
declare const VALIDATION_MESSAGE_INVALID_VALIDATOR_SUFFIX = " at index <>";
|
|
27
|
+
declare const VALIDATION_MESSAGE_INVALID_VALUE_TYPE = "Expected <> but received <>";
|
|
28
|
+
declare const VALIDATION_MESSAGE_INVALID_VALUE_VALIDATOR = "Value does not satisfy validator for type '<>'";
|
|
25
29
|
declare const VALIDATION_MESSAGE_UNKNOWN_KEYS = "Found keys that are not defined in the schema: <>";
|
|
26
30
|
declare const REPORTING_ALL: ReportingType;
|
|
27
31
|
declare const REPORTING_FIRST: ReportingType;
|
|
28
32
|
declare const REPORTING_NONE: ReportingType;
|
|
33
|
+
declare const REPORTING_RESULT: ReportingType;
|
|
29
34
|
declare const REPORTING_THROW: ReportingType;
|
|
30
35
|
declare const REPORTING_TYPES: Set<ReportingType>;
|
|
31
36
|
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED = "'<>' has a default value but is not required";
|
|
@@ -47,7 +52,10 @@ declare const TYPE_NULL = "null";
|
|
|
47
52
|
declare const TYPE_OBJECT = "object";
|
|
48
53
|
declare const TYPE_UNDEFINED = "undefined";
|
|
49
54
|
declare const VALIDATABLE_TYPES: Set<keyof Values>;
|
|
50
|
-
declare const
|
|
51
|
-
declare const
|
|
55
|
+
declare const TYPES_ALL: Set<keyof Values>;
|
|
56
|
+
declare const TYPES_PREFIXED: Record<ValueType, string>;
|
|
57
|
+
declare const VALIDATOR_MESSAGE_INVALID_PROPERTY_NULLABLE = "Validator must not be 'null' or 'undefined'";
|
|
58
|
+
declare const VALIDATOR_MESSAGE_INVALID_PROPERTY_TYPE = "Validator must be of a valid type";
|
|
59
|
+
declare const VALIDATOR_MESSAGE_INVALID_VALIDATOR = "Validator must be a function or an array of functions";
|
|
52
60
|
//#endregion
|
|
53
|
-
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_SCHEMA, NAME_SCHEMA_PREFIXED,
|
|
61
|
+
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_ERROR_VALIDATOR, NAME_SCHEMA, NAME_SCHEMA_PREFIXED, PROPERTY_DEFAULT, PROPERTY_REQUIRED, PROPERTY_SCHEMA, PROPERTY_TYPE, PROPERTY_VALIDATOR, PROPERTY_VALIDATORS, REPORTING_ALL, REPORTING_FIRST, REPORTING_NONE, REPORTING_RESULT, REPORTING_THROW, REPORTING_TYPES, SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_TYPE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_KEY, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE, TEMPLATE_PATTERN, TYPES_ALL, TYPES_PREFIXED, TYPE_ARRAY, TYPE_FUNCTION, TYPE_FUNCTION_RESULT, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES, VALIDATION_MESSAGE_INVALID_INPUT, VALIDATION_MESSAGE_INVALID_PROPERTY_TYPE, VALIDATION_MESSAGE_INVALID_PROPERTY_VALIDATOR, VALIDATION_MESSAGE_INVALID_REQUIRED, VALIDATION_MESSAGE_INVALID_VALIDATOR_SUFFIX, VALIDATION_MESSAGE_INVALID_VALUE_TYPE, VALIDATION_MESSAGE_INVALID_VALUE_VALIDATOR, VALIDATION_MESSAGE_UNKNOWN_KEYS, VALIDATOR_MESSAGE_INVALID_PROPERTY_NULLABLE, VALIDATOR_MESSAGE_INVALID_PROPERTY_TYPE, VALIDATOR_MESSAGE_INVALID_VALIDATOR };
|
package/dist/constants.mjs
CHANGED
|
@@ -9,25 +9,31 @@ const NAME_SCHEMA = "Schema";
|
|
|
9
9
|
const NAME_SCHEMA_PREFIXED = "a Schema";
|
|
10
10
|
const NAME_ERROR_SCHEMATIC = "SchematicError";
|
|
11
11
|
const NAME_ERROR_VALIDATION = "ValidationError";
|
|
12
|
+
const NAME_ERROR_VALIDATOR = "ValidatorError";
|
|
12
13
|
const PROPERTY_DEFAULT = "$default";
|
|
13
14
|
const PROPERTY_REQUIRED = "$required";
|
|
14
15
|
const PROPERTY_SCHEMA = "$schema";
|
|
15
16
|
const PROPERTY_TYPE = "$type";
|
|
17
|
+
const PROPERTY_VALIDATOR = "$validator";
|
|
16
18
|
const PROPERTY_VALIDATORS = "$validators";
|
|
17
19
|
const VALIDATION_MESSAGE_INVALID_INPUT = "Expected an object as input but received <>";
|
|
18
20
|
const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '<>'";
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
21
|
+
const VALIDATION_MESSAGE_INVALID_PROPERTY_TYPE = "Expected <> for '<>' but received <>";
|
|
22
|
+
const VALIDATION_MESSAGE_INVALID_PROPERTY_VALIDATOR = "Value does not satisfy validator for '<>' and type '<>'";
|
|
23
|
+
const VALIDATION_MESSAGE_INVALID_VALIDATOR_SUFFIX = " at index <>";
|
|
24
|
+
const VALIDATION_MESSAGE_INVALID_VALUE_TYPE = "Expected <> but received <>";
|
|
25
|
+
const VALIDATION_MESSAGE_INVALID_VALUE_VALIDATOR = "Value does not satisfy validator for type '<>'";
|
|
22
26
|
const VALIDATION_MESSAGE_UNKNOWN_KEYS = "Found keys that are not defined in the schema: <>";
|
|
23
27
|
const REPORTING_ALL = "all";
|
|
24
28
|
const REPORTING_FIRST = "first";
|
|
25
29
|
const REPORTING_NONE = "none";
|
|
30
|
+
const REPORTING_RESULT = "result";
|
|
26
31
|
const REPORTING_THROW = "throw";
|
|
27
32
|
const REPORTING_TYPES = new Set([
|
|
28
33
|
"all",
|
|
29
34
|
REPORTING_FIRST,
|
|
30
35
|
REPORTING_NONE,
|
|
36
|
+
REPORTING_RESULT,
|
|
31
37
|
REPORTING_THROW
|
|
32
38
|
]);
|
|
33
39
|
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED = "'<>' has a default value but is not required";
|
|
@@ -59,12 +65,12 @@ const VALIDATABLE_TYPES = new Set([
|
|
|
59
65
|
"string",
|
|
60
66
|
"symbol"
|
|
61
67
|
]);
|
|
62
|
-
const
|
|
68
|
+
const TYPES_ALL = new Set([
|
|
63
69
|
...VALIDATABLE_TYPES,
|
|
64
70
|
TYPE_NULL,
|
|
65
71
|
TYPE_UNDEFINED
|
|
66
72
|
]);
|
|
67
|
-
const
|
|
73
|
+
const TYPES_PREFIXED = {
|
|
68
74
|
[TYPE_ARRAY]: `an ${TYPE_ARRAY}`,
|
|
69
75
|
bigint: `a bigint`,
|
|
70
76
|
boolean: `a boolean`,
|
|
@@ -77,5 +83,8 @@ const PREFIXED_TYPES = {
|
|
|
77
83
|
[TYPE_OBJECT]: `an ${TYPE_OBJECT}`,
|
|
78
84
|
[TYPE_UNDEFINED]: TYPE_UNDEFINED
|
|
79
85
|
};
|
|
86
|
+
const VALIDATOR_MESSAGE_INVALID_PROPERTY_NULLABLE = "Validator must not be 'null' or 'undefined'";
|
|
87
|
+
const VALIDATOR_MESSAGE_INVALID_PROPERTY_TYPE = "Validator must be of a valid type";
|
|
88
|
+
const VALIDATOR_MESSAGE_INVALID_VALIDATOR = "Validator must be a function or an array of functions";
|
|
80
89
|
//#endregion
|
|
81
|
-
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_SCHEMA, NAME_SCHEMA_PREFIXED,
|
|
90
|
+
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_ERROR_VALIDATOR, NAME_SCHEMA, NAME_SCHEMA_PREFIXED, PROPERTY_DEFAULT, PROPERTY_REQUIRED, PROPERTY_SCHEMA, PROPERTY_TYPE, PROPERTY_VALIDATOR, PROPERTY_VALIDATORS, REPORTING_ALL, REPORTING_FIRST, REPORTING_NONE, REPORTING_RESULT, REPORTING_THROW, REPORTING_TYPES, SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_TYPE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_TYPE, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_KEY, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE, TEMPLATE_PATTERN, TYPES_ALL, TYPES_PREFIXED, TYPE_ARRAY, TYPE_FUNCTION, TYPE_FUNCTION_RESULT, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES, VALIDATION_MESSAGE_INVALID_INPUT, VALIDATION_MESSAGE_INVALID_PROPERTY_TYPE, VALIDATION_MESSAGE_INVALID_PROPERTY_VALIDATOR, VALIDATION_MESSAGE_INVALID_REQUIRED, VALIDATION_MESSAGE_INVALID_VALIDATOR_SUFFIX, VALIDATION_MESSAGE_INVALID_VALUE_TYPE, VALIDATION_MESSAGE_INVALID_VALUE_VALIDATOR, VALIDATION_MESSAGE_UNKNOWN_KEYS, VALIDATOR_MESSAGE_INVALID_PROPERTY_NULLABLE, VALIDATOR_MESSAGE_INVALID_PROPERTY_TYPE, VALIDATOR_MESSAGE_INVALID_VALIDATOR };
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
//#region src/
|
|
2
|
-
function
|
|
3
|
-
const { length } =
|
|
1
|
+
//#region src/handler/base.handler.ts
|
|
2
|
+
function getBaseHandler(handlers) {
|
|
3
|
+
const { length } = handlers;
|
|
4
4
|
return (input, parameters, get) => {
|
|
5
5
|
const allInformation = [];
|
|
6
6
|
for (let index = 0; index < length; index += 1) {
|
|
7
7
|
const previousInformation = parameters.information;
|
|
8
8
|
parameters.information = [];
|
|
9
|
-
const result =
|
|
9
|
+
const result = handlers[index](input, parameters, get);
|
|
10
10
|
parameters.information = previousInformation;
|
|
11
11
|
if (result === true) return true;
|
|
12
12
|
parameters.information?.push(...result);
|
|
@@ -16,4 +16,4 @@ function getBaseValidator(validators) {
|
|
|
16
16
|
};
|
|
17
17
|
}
|
|
18
18
|
//#endregion
|
|
19
|
-
export {
|
|
19
|
+
export { getBaseHandler };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { instanceOf } from "../helpers/misc.helper.mjs";
|
|
2
|
+
import { isConstructor } from "@oscarpalmer/atoms/is";
|
|
3
|
+
//#region src/handler/function.handler.ts
|
|
4
|
+
function getFunctionHandler(fn) {
|
|
5
|
+
const handler = isConstructor(fn) ? instanceOf(fn) : fn;
|
|
6
|
+
return (input) => handler(input) ? true : [];
|
|
7
|
+
}
|
|
8
|
+
//#endregion
|
|
9
|
+
export { getFunctionHandler };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PropertyValidationKey, ValidationHandler } from "../models/validation.model.mjs";
|
|
2
|
+
import { PlainObject } from "@oscarpalmer/atoms/models";
|
|
3
|
+
|
|
4
|
+
//#region src/handler/object.handler.d.ts
|
|
5
|
+
declare function getObjectHandler(original: PlainObject, origin?: PropertyValidationKey, fromType?: boolean): ValidationHandler;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { getObjectHandler };
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { PROPERTY_DEFAULT, PROPERTY_REQUIRED, PROPERTY_TYPE, PROPERTY_VALIDATORS, SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY } from "../constants.mjs";
|
|
2
|
+
import { SchematicError, ValidationError } from "../models/validation.model.mjs";
|
|
3
|
+
import { report } from "../helpers/report.helper.mjs";
|
|
4
|
+
import { getValueHandler } from "./value.handler.mjs";
|
|
5
|
+
import { getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputTypeMessage, getUnknownKeysMessage } from "../helpers/message.helper.mjs";
|
|
6
|
+
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
7
|
+
import { clone } from "@oscarpalmer/atoms/value/clone";
|
|
8
|
+
//#region src/handler/object.handler.ts
|
|
9
|
+
function getDisallowedProperty(obj) {
|
|
10
|
+
if ("$default" in obj) return PROPERTY_DEFAULT;
|
|
11
|
+
if ("$required" in obj) return PROPERTY_REQUIRED;
|
|
12
|
+
if ("$type" in obj) return PROPERTY_TYPE;
|
|
13
|
+
if ("$validators" in obj) return PROPERTY_VALIDATORS;
|
|
14
|
+
}
|
|
15
|
+
function getObjectHandler(original, origin, fromType) {
|
|
16
|
+
const keys = Object.keys(original);
|
|
17
|
+
const keysLength = keys.length;
|
|
18
|
+
if (keysLength === 0) throw new SchematicError(SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY);
|
|
19
|
+
if (fromType ?? false) {
|
|
20
|
+
const property = getDisallowedProperty(original);
|
|
21
|
+
if (property != null) throw new SchematicError(getDisallowedMessage(origin.full, property));
|
|
22
|
+
}
|
|
23
|
+
const set = /* @__PURE__ */ new Set();
|
|
24
|
+
const items = [];
|
|
25
|
+
for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
26
|
+
const key = keys[keyIndex];
|
|
27
|
+
const { defaults, handler, key: fullKey, required, types } = getValueHandler({ value: original[key] }, {
|
|
28
|
+
key,
|
|
29
|
+
origin
|
|
30
|
+
});
|
|
31
|
+
items.push({
|
|
32
|
+
defaults,
|
|
33
|
+
handler,
|
|
34
|
+
required,
|
|
35
|
+
types,
|
|
36
|
+
key: fullKey
|
|
37
|
+
});
|
|
38
|
+
set.add(key);
|
|
39
|
+
}
|
|
40
|
+
const validatorsLength = items.length;
|
|
41
|
+
return (input, parameters, get) => {
|
|
42
|
+
if (!isPlainObject(input)) return origin == null ? report({
|
|
43
|
+
message: {
|
|
44
|
+
arguments: [input],
|
|
45
|
+
callback: getInputTypeMessage
|
|
46
|
+
},
|
|
47
|
+
original: parameters,
|
|
48
|
+
value: input
|
|
49
|
+
}, true) : [];
|
|
50
|
+
if (parameters.strict) {
|
|
51
|
+
const unknownKeys = Object.keys(input).filter((key) => !set.has(key));
|
|
52
|
+
if (unknownKeys.length > 0) {
|
|
53
|
+
const information = {
|
|
54
|
+
key: origin,
|
|
55
|
+
message: getUnknownKeysMessage(unknownKeys),
|
|
56
|
+
value: input
|
|
57
|
+
};
|
|
58
|
+
if (parameters.reporting.throw) throw new ValidationError([information]);
|
|
59
|
+
parameters.information?.push(information);
|
|
60
|
+
return [information];
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const getAndClone = get && parameters.clone;
|
|
64
|
+
const allInformation = [];
|
|
65
|
+
const output = {};
|
|
66
|
+
for (let validatorIndex = 0; validatorIndex < validatorsLength; validatorIndex += 1) {
|
|
67
|
+
const { defaults, handler, key, required, types } = items[validatorIndex];
|
|
68
|
+
const value = input[key.short];
|
|
69
|
+
if (value === void 0) {
|
|
70
|
+
if (required) {
|
|
71
|
+
if (get && defaults != null) {
|
|
72
|
+
const defaultValue = clone(defaults.value);
|
|
73
|
+
if (parameters.clone) output[key.short] = defaultValue;
|
|
74
|
+
else input[key.short] = defaultValue;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (parameters.reporting.none) return [];
|
|
78
|
+
const reported = report({
|
|
79
|
+
key,
|
|
80
|
+
value,
|
|
81
|
+
information: { all: allInformation },
|
|
82
|
+
message: {
|
|
83
|
+
arguments: [key.full, types],
|
|
84
|
+
callback: getInputPropertyMissingMessage
|
|
85
|
+
},
|
|
86
|
+
original: parameters
|
|
87
|
+
});
|
|
88
|
+
if (reported == null) continue;
|
|
89
|
+
return reported;
|
|
90
|
+
}
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
const previousOutput = parameters.output;
|
|
94
|
+
parameters.key = key.full;
|
|
95
|
+
parameters.output = output;
|
|
96
|
+
const result = handler(value, parameters, get);
|
|
97
|
+
parameters.output = previousOutput;
|
|
98
|
+
if (result === true) {
|
|
99
|
+
if (getAndClone && !isPlainObject(value)) output[key.short] = clone(value);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (parameters.reporting.none) return [];
|
|
103
|
+
const reported = report({
|
|
104
|
+
key,
|
|
105
|
+
value,
|
|
106
|
+
extract: false,
|
|
107
|
+
information: {
|
|
108
|
+
all: allInformation,
|
|
109
|
+
existing: typeof result !== "boolean" && result.length > 0 ? result : void 0
|
|
110
|
+
},
|
|
111
|
+
message: {
|
|
112
|
+
arguments: [
|
|
113
|
+
key.full,
|
|
114
|
+
types,
|
|
115
|
+
value
|
|
116
|
+
],
|
|
117
|
+
callback: getInputPropertyTypeMessage
|
|
118
|
+
},
|
|
119
|
+
original: parameters
|
|
120
|
+
});
|
|
121
|
+
if (reported == null) continue;
|
|
122
|
+
return reported;
|
|
123
|
+
}
|
|
124
|
+
if (getAndClone) if (origin == null) parameters.output = output;
|
|
125
|
+
else parameters.output[origin.short] = output;
|
|
126
|
+
return allInformation.length === 0 ? true : allInformation;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
export { getObjectHandler };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationHandler } from "../models/validation.model.mjs";
|
|
2
|
+
import { Schema } from "../schema.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/handler/schema.handler.d.ts
|
|
5
|
+
declare function getSchemaHandler(schematic: Schema<unknown>): ValidationHandler;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { getSchemaHandler };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { schemaHandlers } from "../schema.mjs";
|
|
2
|
+
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
|
+
//#region src/handler/schema.handler.ts
|
|
4
|
+
function getSchemaHandler(schematic) {
|
|
5
|
+
const handler = schemaHandlers.get(schematic);
|
|
6
|
+
return (input, parameters, get) => {
|
|
7
|
+
let result;
|
|
8
|
+
if (isPlainObject(input)) result = handler(input, parameters, get);
|
|
9
|
+
else result = [];
|
|
10
|
+
if (result === true) return result;
|
|
11
|
+
parameters.information?.push(...result);
|
|
12
|
+
return result;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
export { getSchemaHandler };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PropertyValidationKey, ValidationHandler, ValidationHandlerType, Validators } from "../models/validation.model.mjs";
|
|
2
|
+
import { ValueType } from "../models/misc.model.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/handler/type.handler.d.ts
|
|
5
|
+
declare function getTypeHandler(type: ValueType, validators: Validators, key?: PropertyValidationKey): ValidationHandler;
|
|
6
|
+
declare function getTypeValidators(types: ValidationHandlerType[], original: unknown): Validators;
|
|
7
|
+
declare function getValidators(original: unknown, allowed: boolean, prefix?: string): Validators;
|
|
8
|
+
//#endregion
|
|
9
|
+
export { getTypeHandler, getTypeValidators, getValidators };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { PROPERTY_VALIDATORS, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_KEY, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE, SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE, TYPES_ALL, VALIDATOR_MESSAGE_INVALID_VALIDATOR } from "../constants.mjs";
|
|
2
|
+
import { SchematicError, ValidatorError } from "../models/validation.model.mjs";
|
|
3
|
+
import { getInputPropertyValidatorMessage, getInputValueValidatorMessage } from "../helpers/message.helper.mjs";
|
|
4
|
+
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
5
|
+
//#region src/handler/type.handler.ts
|
|
6
|
+
function getTypeHandler(type, validators, key) {
|
|
7
|
+
const validator = typeValidators[type];
|
|
8
|
+
const typedValidators = validators[type] ?? [];
|
|
9
|
+
const { length } = typedValidators;
|
|
10
|
+
return (input, parameters) => {
|
|
11
|
+
if (!validator(input)) return [];
|
|
12
|
+
for (let index = 0; index < length; index += 1) {
|
|
13
|
+
const validator = typedValidators[index];
|
|
14
|
+
if (validator(input) === true) continue;
|
|
15
|
+
const information = {
|
|
16
|
+
validator,
|
|
17
|
+
message: key == null ? getInputValueValidatorMessage(type, index, length) : getInputPropertyValidatorMessage(key.full, type, index, length),
|
|
18
|
+
value: input
|
|
19
|
+
};
|
|
20
|
+
if (key != null) information.key = key;
|
|
21
|
+
parameters.information?.push(information);
|
|
22
|
+
return parameters.reporting.none ? [] : [information];
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
function getTypeValidators(types, original) {
|
|
28
|
+
const values = types.filter((type) => TYPES_ALL.has(type));
|
|
29
|
+
const { length } = values;
|
|
30
|
+
const validators = {};
|
|
31
|
+
if (original == null || length === 0) return validators;
|
|
32
|
+
if (typeof original === "function" || Array.isArray(original)) {
|
|
33
|
+
if (length > 1) throw new ValidatorError(SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE);
|
|
34
|
+
return getValidators({ [values[0]]: original }, true);
|
|
35
|
+
}
|
|
36
|
+
return getValidators(original, true);
|
|
37
|
+
}
|
|
38
|
+
function getValidators(original, allowed, prefix) {
|
|
39
|
+
const validators = {};
|
|
40
|
+
if (original == null) return validators;
|
|
41
|
+
if (!allowed) throw new SchematicError(SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED.replace("<>", prefix).replace("<>", PROPERTY_VALIDATORS));
|
|
42
|
+
if (!isPlainObject(original)) throw new SchematicError(SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE);
|
|
43
|
+
const keys = Object.keys(original);
|
|
44
|
+
const { length } = keys;
|
|
45
|
+
for (let index = 0; index < length; index += 1) {
|
|
46
|
+
const key = keys[index];
|
|
47
|
+
if (!TYPES_ALL.has(key)) throw new SchematicError(SCHEMATIC_MESSAGE_VALIDATOR_INVALID_KEY.replace("<>", key));
|
|
48
|
+
const value = original[key];
|
|
49
|
+
validators[key] = (Array.isArray(value) ? value : [value]).map((item) => {
|
|
50
|
+
if (typeof item !== "function") if (prefix == null) throw new ValidatorError(VALIDATOR_MESSAGE_INVALID_VALIDATOR);
|
|
51
|
+
else throw new SchematicError(SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE.replace("<>", key).replace("<>", prefix));
|
|
52
|
+
return item;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return validators;
|
|
56
|
+
}
|
|
57
|
+
const typeValidators = {
|
|
58
|
+
array: Array.isArray,
|
|
59
|
+
bigint: (value) => typeof value === "bigint",
|
|
60
|
+
boolean: (value) => typeof value === "boolean",
|
|
61
|
+
date: (value) => value instanceof Date,
|
|
62
|
+
function: (value) => typeof value === "function",
|
|
63
|
+
null: (value) => value === null,
|
|
64
|
+
number: (value) => typeof value === "number",
|
|
65
|
+
object: (value) => typeof value === "object" && value !== null,
|
|
66
|
+
string: (value) => typeof value === "string",
|
|
67
|
+
symbol: (value) => typeof value === "symbol",
|
|
68
|
+
undefined: (value) => value === void 0
|
|
69
|
+
};
|
|
70
|
+
//#endregion
|
|
71
|
+
export { getTypeHandler, getTypeValidators, getValidators };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ValidationHandler, ValidationHandlerType } from "../models/validation.model.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/handler/validator.handler.d.ts
|
|
4
|
+
type ValidatorHandler = {
|
|
5
|
+
handler: ValidationHandler;
|
|
6
|
+
types: ValidationHandlerType[];
|
|
7
|
+
};
|
|
8
|
+
declare function getValidatorHandler(value: unknown, validators?: unknown): ValidatorHandler;
|
|
9
|
+
//#endregion
|
|
10
|
+
export { getValidatorHandler };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { report } from "../helpers/report.helper.mjs";
|
|
2
|
+
import { getValueHandler } from "./value.handler.mjs";
|
|
3
|
+
import { getInputValueTypeMessage } from "../helpers/message.helper.mjs";
|
|
4
|
+
//#region src/handler/validator.handler.ts
|
|
5
|
+
function getValidatorHandler(value, validators) {
|
|
6
|
+
const { handler, types } = getValueHandler({
|
|
7
|
+
validators,
|
|
8
|
+
value
|
|
9
|
+
});
|
|
10
|
+
const validator = (input, parameters, get) => {
|
|
11
|
+
const result = handler(input, parameters, get);
|
|
12
|
+
if (result === true) return true;
|
|
13
|
+
if (parameters.key != null) return [];
|
|
14
|
+
return report({
|
|
15
|
+
value: input,
|
|
16
|
+
extract: false,
|
|
17
|
+
information: {
|
|
18
|
+
all: parameters.information ?? [],
|
|
19
|
+
existing: result.length > 0 ? result : void 0
|
|
20
|
+
},
|
|
21
|
+
message: {
|
|
22
|
+
arguments: [types, input],
|
|
23
|
+
callback: getInputValueTypeMessage
|
|
24
|
+
},
|
|
25
|
+
original: parameters
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
types,
|
|
30
|
+
handler: validator
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//#endregion
|
|
34
|
+
export { getValidatorHandler };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { PropertyValidationKey, ValidationHandlerItem } from "../models/validation.model.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/handler/value.handler.d.ts
|
|
4
|
+
type Input = {
|
|
5
|
+
validators?: unknown;
|
|
6
|
+
value: unknown;
|
|
7
|
+
};
|
|
8
|
+
type PropertyInformation = {
|
|
9
|
+
key: string;
|
|
10
|
+
origin?: PropertyValidationKey;
|
|
11
|
+
};
|
|
12
|
+
declare function getValueHandler(input: Input, property?: PropertyInformation): ValidationHandlerItem;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { getValueHandler };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { PROPERTY_DEFAULT, PROPERTY_REQUIRED, PROPERTY_TYPE, PROPERTY_VALIDATORS, TYPES_ALL, VALIDATOR_MESSAGE_INVALID_PROPERTY_NULLABLE, VALIDATOR_MESSAGE_INVALID_PROPERTY_TYPE } from "../constants.mjs";
|
|
2
|
+
import { getParameters, isSchema, isValidator } from "../helpers/misc.helper.mjs";
|
|
3
|
+
import { SchematicError, ValidatorError } from "../models/validation.model.mjs";
|
|
4
|
+
import { getBaseHandler } from "./base.handler.mjs";
|
|
5
|
+
import { getFunctionHandler } from "./function.handler.mjs";
|
|
6
|
+
import { getSchemaHandler } from "./schema.handler.mjs";
|
|
7
|
+
import { getTypeHandler, getTypeValidators, getValidators } from "./type.handler.mjs";
|
|
8
|
+
import { validatorHandlers } from "../validator.mjs";
|
|
9
|
+
import { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage } from "../helpers/message.helper.mjs";
|
|
10
|
+
import { getObjectHandler } from "./object.handler.mjs";
|
|
11
|
+
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
12
|
+
import { join } from "@oscarpalmer/atoms/string";
|
|
13
|
+
//#region src/handler/value.handler.ts
|
|
14
|
+
function getDefaults(obj, key, allowed) {
|
|
15
|
+
if (!("$default" in obj)) return;
|
|
16
|
+
if (!allowed) throw new SchematicError(getDisallowedMessage(key, PROPERTY_DEFAULT));
|
|
17
|
+
return { value: obj[PROPERTY_DEFAULT] };
|
|
18
|
+
}
|
|
19
|
+
function getRequired(obj, key, allowed) {
|
|
20
|
+
if (!("$required" in obj)) return;
|
|
21
|
+
if (!allowed) throw new SchematicError(getDisallowedMessage(key, PROPERTY_REQUIRED));
|
|
22
|
+
if (typeof obj["$required"] !== "boolean") throw new SchematicError(getRequiredMessage(key));
|
|
23
|
+
return obj[PROPERTY_REQUIRED];
|
|
24
|
+
}
|
|
25
|
+
function getValueHandler(input, property) {
|
|
26
|
+
const isProperty = property != null;
|
|
27
|
+
const prefixedKey = isProperty ? property.origin == null ? property.key : join([property.origin.full, property.key], ".") : "";
|
|
28
|
+
const { value } = input;
|
|
29
|
+
if (value == null) {
|
|
30
|
+
if (isProperty) throw new SchematicError(getSchematicPropertyNullableMessage(prefixedKey));
|
|
31
|
+
throw new ValidatorError(VALIDATOR_MESSAGE_INVALID_PROPERTY_NULLABLE);
|
|
32
|
+
}
|
|
33
|
+
const fullKey = {
|
|
34
|
+
full: prefixedKey,
|
|
35
|
+
short: property?.key ?? ""
|
|
36
|
+
};
|
|
37
|
+
let required = true;
|
|
38
|
+
let typed = false;
|
|
39
|
+
let validators = {};
|
|
40
|
+
let defaults;
|
|
41
|
+
let types;
|
|
42
|
+
const handlers = [];
|
|
43
|
+
if (isProperty && isPlainObject(value)) {
|
|
44
|
+
typed = PROPERTY_TYPE in value;
|
|
45
|
+
const type = typed ? value[PROPERTY_TYPE] : value;
|
|
46
|
+
defaults = getDefaults(value, prefixedKey, typed);
|
|
47
|
+
required = getRequired(value, prefixedKey, typed) ?? required;
|
|
48
|
+
validators = getValidators(value[PROPERTY_VALIDATORS], typed, prefixedKey);
|
|
49
|
+
types = Array.isArray(type) ? type : [type];
|
|
50
|
+
} else {
|
|
51
|
+
types = Array.isArray(value) ? value : [value];
|
|
52
|
+
if (input.validators != null) validators = getTypeValidators(types, input.validators);
|
|
53
|
+
}
|
|
54
|
+
const typesLength = types.length;
|
|
55
|
+
let invalid = false;
|
|
56
|
+
typeLoop: for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
57
|
+
const type = types[typeIndex];
|
|
58
|
+
let handler;
|
|
59
|
+
switch (true) {
|
|
60
|
+
case typeof type === "function":
|
|
61
|
+
handler = getFunctionHandler(type);
|
|
62
|
+
break;
|
|
63
|
+
case isProperty && isPlainObject(type):
|
|
64
|
+
handler = getObjectHandler(type, fullKey, typed);
|
|
65
|
+
break;
|
|
66
|
+
case isProperty && isSchema(type):
|
|
67
|
+
handler = getSchemaHandler(type);
|
|
68
|
+
break;
|
|
69
|
+
case isProperty && isValidator(type):
|
|
70
|
+
handler = validatorHandlers.get(type);
|
|
71
|
+
break;
|
|
72
|
+
case TYPES_ALL.has(type):
|
|
73
|
+
handler = getTypeHandler(type, validators, isProperty ? fullKey : void 0);
|
|
74
|
+
break;
|
|
75
|
+
default:
|
|
76
|
+
invalid = true;
|
|
77
|
+
break typeLoop;
|
|
78
|
+
}
|
|
79
|
+
handlers.push(handler);
|
|
80
|
+
}
|
|
81
|
+
if (invalid || handlers.length === 0) {
|
|
82
|
+
if (isProperty) throw new SchematicError(getSchematicPropertyTypeMessage(prefixedKey));
|
|
83
|
+
throw new ValidatorError(VALIDATOR_MESSAGE_INVALID_PROPERTY_TYPE);
|
|
84
|
+
}
|
|
85
|
+
required = required && !types.includes("undefined");
|
|
86
|
+
if (defaults != null && !required) throw new SchematicError(getDefaultRequiredMessage(prefixedKey));
|
|
87
|
+
const handler = getBaseHandler(handlers);
|
|
88
|
+
if (defaults != null && Array.isArray(handler(defaults.value, getParameters(isProperty), false))) throw new SchematicError(getDefaultTypeMessage(prefixedKey, types));
|
|
89
|
+
return {
|
|
90
|
+
defaults,
|
|
91
|
+
handler,
|
|
92
|
+
required,
|
|
93
|
+
types,
|
|
94
|
+
key: fullKey
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
//#endregion
|
|
98
|
+
export { getValueHandler };
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ValidationHandlerType } from "../models/validation.model.mjs";
|
|
2
|
+
import { ValueType } from "../models/misc.model.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/helpers/message.helper.d.ts
|
|
5
5
|
declare function getDefaultRequiredMessage(key: string): string;
|
|
6
|
-
declare function getDefaultTypeMessage(key: string, types:
|
|
6
|
+
declare function getDefaultTypeMessage(key: string, types: ValidationHandlerType[]): string;
|
|
7
7
|
declare function getDisallowedMessage(key: string, property: string): string;
|
|
8
8
|
declare function getInputTypeMessage(actual: unknown): string;
|
|
9
|
-
declare function getInputPropertyMissingMessage(key: string, types:
|
|
10
|
-
declare function getInputPropertyTypeMessage(key: string, types:
|
|
11
|
-
declare function getInputPropertyValidatorMessage(key: string, type:
|
|
9
|
+
declare function getInputPropertyMissingMessage(key: string, types: ValidationHandlerType[]): string;
|
|
10
|
+
declare function getInputPropertyTypeMessage(key: string, types: ValidationHandlerType[], actual: unknown): string;
|
|
11
|
+
declare function getInputPropertyValidatorMessage(key: string, type: ValueType, index: number, length: number): string;
|
|
12
|
+
declare function getInputValueTypeMessage(types: ValidationHandlerType[], actual: unknown): string;
|
|
13
|
+
declare function getInputValueValidatorMessage(type: ValueType, index: number, length: number): string;
|
|
12
14
|
declare function getSchematicPropertyNullableMessage(key: string): string;
|
|
13
15
|
declare function getSchematicPropertyTypeMessage(key: string): string;
|
|
14
16
|
declare function getRequiredMessage(key: string): string;
|
|
15
17
|
declare function getUnknownKeysMessage(keys: string[]): string;
|
|
16
18
|
//#endregion
|
|
17
|
-
export { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputPropertyValidatorMessage, getInputTypeMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage, getUnknownKeysMessage };
|
|
19
|
+
export { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputPropertyValidatorMessage, getInputTypeMessage, getInputValueTypeMessage, getInputValueValidatorMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage, getUnknownKeysMessage };
|