@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
package/dist/constants.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ReportingType } from "./models/validation.model.mjs";
|
|
2
|
-
import { Values } from "./models/misc.model.mjs";
|
|
2
|
+
import { ValueName, Values } from "./models/misc.model.mjs";
|
|
3
3
|
|
|
4
4
|
//#region src/constants.d.ts
|
|
5
5
|
declare const COMMA = ", ";
|
|
@@ -9,13 +9,15 @@ declare const CONJUNCTION_AND = " and ";
|
|
|
9
9
|
declare const CONJUNCTION_AND_COMMA = ", and ";
|
|
10
10
|
declare const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
|
|
11
11
|
declare const NAME_SCHEMATIC = "Schematic";
|
|
12
|
+
declare const NAME_SCHEMATIC_PREFIXED = "a Schematic";
|
|
12
13
|
declare const NAME_ERROR_SCHEMATIC = "SchematicError";
|
|
13
14
|
declare const NAME_ERROR_VALIDATION = "ValidationError";
|
|
15
|
+
declare const PROPERTY_DEFAULT = "$default";
|
|
14
16
|
declare const PROPERTY_REQUIRED = "$required";
|
|
15
17
|
declare const PROPERTY_SCHEMATIC = "$schematic";
|
|
16
18
|
declare const PROPERTY_TYPE = "$type";
|
|
17
19
|
declare const PROPERTY_VALIDATORS = "$validators";
|
|
18
|
-
declare const VALIDATION_MESSAGE_INVALID_INPUT = "Expected
|
|
20
|
+
declare const VALIDATION_MESSAGE_INVALID_INPUT = "Expected an object as input but received <>";
|
|
19
21
|
declare const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '<>'";
|
|
20
22
|
declare const VALIDATION_MESSAGE_INVALID_TYPE = "Expected <> for '<>' but received <>";
|
|
21
23
|
declare const VALIDATION_MESSAGE_INVALID_VALUE = "Value does not satisfy validator for '<>' and type '<>'";
|
|
@@ -26,8 +28,10 @@ declare const REPORTING_FIRST: ReportingType;
|
|
|
26
28
|
declare const REPORTING_NONE: ReportingType;
|
|
27
29
|
declare const REPORTING_THROW: ReportingType;
|
|
28
30
|
declare const REPORTING_TYPES: Set<ReportingType>;
|
|
31
|
+
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED = "'<>' has a default value but is not required";
|
|
32
|
+
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_TYPE = "Expected default value for property '<>' to be <>";
|
|
29
33
|
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY = "Schema must have at least one property";
|
|
30
|
-
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<>.<>' property is not allowed for schemas
|
|
34
|
+
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<>.<>' property is not allowed for plain schemas";
|
|
31
35
|
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE = "'<>' property must not be 'null' or 'undefined'";
|
|
32
36
|
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
|
|
33
37
|
declare const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
@@ -37,10 +41,13 @@ declare const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE = "Validators must be an
|
|
|
37
41
|
declare const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE = "Validator '<>' must be a function or an array of functions";
|
|
38
42
|
declare const TEMPLATE_PATTERN = "<>";
|
|
39
43
|
declare const TYPE_ARRAY = "array";
|
|
44
|
+
declare const TYPE_FUNCTION = "function";
|
|
45
|
+
declare const TYPE_FUNCTION_RESULT = "a validated value";
|
|
40
46
|
declare const TYPE_NULL = "null";
|
|
41
47
|
declare const TYPE_OBJECT = "object";
|
|
42
48
|
declare const TYPE_UNDEFINED = "undefined";
|
|
43
49
|
declare const VALIDATABLE_TYPES: Set<keyof Values>;
|
|
44
50
|
declare const TYPE_ALL: Set<keyof Values>;
|
|
51
|
+
declare const PREFIXED_TYPES: Record<ValueName, string>;
|
|
45
52
|
//#endregion
|
|
46
|
-
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_SCHEMATIC, PROPERTY_REQUIRED, PROPERTY_SCHEMATIC, PROPERTY_TYPE, PROPERTY_VALIDATORS, REPORTING_ALL, REPORTING_FIRST, REPORTING_NONE, REPORTING_THROW, REPORTING_TYPES, 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, TYPE_ALL, TYPE_ARRAY, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES, 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 };
|
|
53
|
+
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_SCHEMATIC, NAME_SCHEMATIC_PREFIXED, PREFIXED_TYPES, PROPERTY_DEFAULT, PROPERTY_REQUIRED, PROPERTY_SCHEMATIC, PROPERTY_TYPE, PROPERTY_VALIDATORS, REPORTING_ALL, REPORTING_FIRST, REPORTING_NONE, 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, TYPE_ALL, TYPE_ARRAY, TYPE_FUNCTION, TYPE_FUNCTION_RESULT, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES, 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 };
|
package/dist/constants.mjs
CHANGED
|
@@ -6,13 +6,15 @@ const CONJUNCTION_AND = " and ";
|
|
|
6
6
|
const CONJUNCTION_AND_COMMA = ", and ";
|
|
7
7
|
const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
|
|
8
8
|
const NAME_SCHEMATIC = "Schematic";
|
|
9
|
+
const NAME_SCHEMATIC_PREFIXED = "a Schematic";
|
|
9
10
|
const NAME_ERROR_SCHEMATIC = "SchematicError";
|
|
10
11
|
const NAME_ERROR_VALIDATION = "ValidationError";
|
|
12
|
+
const PROPERTY_DEFAULT = "$default";
|
|
11
13
|
const PROPERTY_REQUIRED = "$required";
|
|
12
14
|
const PROPERTY_SCHEMATIC = "$schematic";
|
|
13
15
|
const PROPERTY_TYPE = "$type";
|
|
14
16
|
const PROPERTY_VALIDATORS = "$validators";
|
|
15
|
-
const VALIDATION_MESSAGE_INVALID_INPUT = "Expected
|
|
17
|
+
const VALIDATION_MESSAGE_INVALID_INPUT = "Expected an object as input but received <>";
|
|
16
18
|
const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '<>'";
|
|
17
19
|
const VALIDATION_MESSAGE_INVALID_TYPE = "Expected <> for '<>' but received <>";
|
|
18
20
|
const VALIDATION_MESSAGE_INVALID_VALUE = "Value does not satisfy validator for '<>' and type '<>'";
|
|
@@ -28,8 +30,10 @@ const REPORTING_TYPES = new Set([
|
|
|
28
30
|
REPORTING_NONE,
|
|
29
31
|
REPORTING_THROW
|
|
30
32
|
]);
|
|
33
|
+
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED = "'<>' has a default value but is not required";
|
|
34
|
+
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_TYPE = "Expected default value for property '<>' to be <>";
|
|
31
35
|
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_EMPTY = "Schema must have at least one property";
|
|
32
|
-
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<>.<>' property is not allowed for schemas
|
|
36
|
+
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<>.<>' property is not allowed for plain schemas";
|
|
33
37
|
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE = "'<>' property must not be 'null' or 'undefined'";
|
|
34
38
|
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
|
|
35
39
|
const SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
@@ -39,24 +43,39 @@ const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE = "Validators must be an object";
|
|
|
39
43
|
const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE = "Validator '<>' must be a function or an array of functions";
|
|
40
44
|
const TEMPLATE_PATTERN = "<>";
|
|
41
45
|
const TYPE_ARRAY = "array";
|
|
46
|
+
const TYPE_FUNCTION = "function";
|
|
47
|
+
const TYPE_FUNCTION_RESULT = "a validated value";
|
|
42
48
|
const TYPE_NULL = "null";
|
|
43
49
|
const TYPE_OBJECT = "object";
|
|
44
50
|
const TYPE_UNDEFINED = "undefined";
|
|
45
51
|
const VALIDATABLE_TYPES = new Set([
|
|
46
|
-
|
|
52
|
+
TYPE_ARRAY,
|
|
47
53
|
"bigint",
|
|
48
54
|
"boolean",
|
|
49
55
|
"date",
|
|
50
|
-
|
|
56
|
+
TYPE_FUNCTION,
|
|
51
57
|
"number",
|
|
58
|
+
TYPE_OBJECT,
|
|
52
59
|
"string",
|
|
53
|
-
"symbol"
|
|
54
|
-
TYPE_OBJECT
|
|
60
|
+
"symbol"
|
|
55
61
|
]);
|
|
56
62
|
const TYPE_ALL = new Set([
|
|
57
63
|
...VALIDATABLE_TYPES,
|
|
58
|
-
|
|
64
|
+
TYPE_NULL,
|
|
59
65
|
TYPE_UNDEFINED
|
|
60
66
|
]);
|
|
67
|
+
const PREFIXED_TYPES = {
|
|
68
|
+
[TYPE_ARRAY]: `an ${TYPE_ARRAY}`,
|
|
69
|
+
bigint: `a bigint`,
|
|
70
|
+
boolean: `a boolean`,
|
|
71
|
+
date: `a date`,
|
|
72
|
+
[TYPE_FUNCTION]: `a ${TYPE_FUNCTION}`,
|
|
73
|
+
[TYPE_NULL]: TYPE_NULL,
|
|
74
|
+
number: `a number`,
|
|
75
|
+
string: `a string`,
|
|
76
|
+
symbol: `a symbol`,
|
|
77
|
+
[TYPE_OBJECT]: `an ${TYPE_OBJECT}`,
|
|
78
|
+
[TYPE_UNDEFINED]: TYPE_UNDEFINED
|
|
79
|
+
};
|
|
61
80
|
//#endregion
|
|
62
|
-
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_SCHEMATIC, PROPERTY_REQUIRED, PROPERTY_SCHEMATIC, PROPERTY_TYPE, PROPERTY_VALIDATORS, REPORTING_ALL, REPORTING_FIRST, REPORTING_NONE, REPORTING_THROW, REPORTING_TYPES, 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, TYPE_ALL, TYPE_ARRAY, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES, 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 };
|
|
81
|
+
export { COMMA, CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_ERROR_SCHEMATIC, NAME_ERROR_VALIDATION, NAME_SCHEMATIC, NAME_SCHEMATIC_PREFIXED, PREFIXED_TYPES, PROPERTY_DEFAULT, PROPERTY_REQUIRED, PROPERTY_SCHEMATIC, PROPERTY_TYPE, PROPERTY_VALIDATORS, REPORTING_ALL, REPORTING_FIRST, REPORTING_NONE, 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, TYPE_ALL, TYPE_ARRAY, TYPE_FUNCTION, TYPE_FUNCTION_RESULT, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES, 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 };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { ValidatorType } from "../models/validation.model.mjs";
|
|
2
|
+
import { ValueName } from "../models/misc.model.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/helpers/message.helper.d.ts
|
|
5
|
+
declare function getDefaultRequiredMessage(key: string): string;
|
|
6
|
+
declare function getDefaultTypeMessage(key: string, types: ValidatorType[]): string;
|
|
7
|
+
declare function getDisallowedMessage(key: string, property: string): string;
|
|
8
|
+
declare function getInputTypeMessage(actual: unknown): string;
|
|
9
|
+
declare function getInputPropertyMissingMessage(key: string, types: ValidatorType[]): string;
|
|
10
|
+
declare function getInputPropertyTypeMessage(key: string, types: ValidatorType[], actual: unknown): string;
|
|
11
|
+
declare function getInputPropertyValidatorMessage(key: string, type: ValueName, index: number, length: number): string;
|
|
12
|
+
declare function getSchematicPropertyNullableMessage(key: string): string;
|
|
13
|
+
declare function getSchematicPropertyTypeMessage(key: string): string;
|
|
14
|
+
declare function getRequiredMessage(key: string): string;
|
|
15
|
+
declare function getUnknownKeysMessage(keys: string[]): string;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputPropertyValidatorMessage, getInputTypeMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage, getUnknownKeysMessage };
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, PREFIXED_TYPES, SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_TYPE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED, SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE, TYPE_ALL, TYPE_ARRAY, TYPE_FUNCTION_RESULT, TYPE_NULL, TYPE_OBJECT, 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/message.helper.ts
|
|
4
|
+
function getDefaultRequiredMessage(key) {
|
|
5
|
+
return SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_REQUIRED.replace("<>", key);
|
|
6
|
+
}
|
|
7
|
+
function getDefaultTypeMessage(key, types) {
|
|
8
|
+
let message = SCHEMATIC_MESSAGE_SCHEMA_INVALID_DEFAULT_TYPE.replace("<>", key);
|
|
9
|
+
message = message.replace("<>", renderTypes(types));
|
|
10
|
+
return message;
|
|
11
|
+
}
|
|
12
|
+
function getDisallowedMessage(key, property) {
|
|
13
|
+
let message = SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED.replace("<>", key);
|
|
14
|
+
message = message.replace("<>", property);
|
|
15
|
+
return message;
|
|
16
|
+
}
|
|
17
|
+
function getInputTypeMessage(actual) {
|
|
18
|
+
return VALIDATION_MESSAGE_INVALID_INPUT.replace("<>", getValueType(actual));
|
|
19
|
+
}
|
|
20
|
+
function getInputPropertyMissingMessage(key, types) {
|
|
21
|
+
let message = VALIDATION_MESSAGE_INVALID_REQUIRED.replace("<>", renderTypes(types));
|
|
22
|
+
message = message.replace("<>", key);
|
|
23
|
+
return message;
|
|
24
|
+
}
|
|
25
|
+
function getInputPropertyTypeMessage(key, types, actual) {
|
|
26
|
+
let message = VALIDATION_MESSAGE_INVALID_TYPE.replace("<>", renderTypes(types));
|
|
27
|
+
message = message.replace("<>", key);
|
|
28
|
+
message = message.replace("<>", getValueType(actual));
|
|
29
|
+
return message;
|
|
30
|
+
}
|
|
31
|
+
function getInputPropertyValidatorMessage(key, type, index, length) {
|
|
32
|
+
let message = VALIDATION_MESSAGE_INVALID_VALUE.replace("<>", key);
|
|
33
|
+
message = message.replace("<>", type);
|
|
34
|
+
if (length > 1) message += VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX.replace("<>", String(index));
|
|
35
|
+
return message;
|
|
36
|
+
}
|
|
37
|
+
function getSchematicPropertyNullableMessage(key) {
|
|
38
|
+
return SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE.replace("<>", key);
|
|
39
|
+
}
|
|
40
|
+
function getSchematicPropertyTypeMessage(key) {
|
|
41
|
+
return SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE.replace("<>", key);
|
|
42
|
+
}
|
|
43
|
+
function getPropertyType(type) {
|
|
44
|
+
switch (true) {
|
|
45
|
+
case typeof type === "function": return isConstructor(type) ? type.name : TYPE_FUNCTION_RESULT;
|
|
46
|
+
case TYPE_ALL.has(type): return PREFIXED_TYPES[type];
|
|
47
|
+
default: return PREFIXED_TYPES[TYPE_OBJECT];
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
function getValueType(value) {
|
|
51
|
+
const valueType = typeof value;
|
|
52
|
+
switch (true) {
|
|
53
|
+
case value === null: return TYPE_NULL;
|
|
54
|
+
case Array.isArray(value): return PREFIXED_TYPES[TYPE_ARRAY];
|
|
55
|
+
case isPlainObject(value): return PREFIXED_TYPES[TYPE_OBJECT];
|
|
56
|
+
case valueType !== TYPE_OBJECT: return PREFIXED_TYPES[valueType];
|
|
57
|
+
default: return value.constructor.name;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
function renderKeys(keys) {
|
|
61
|
+
return renderParts(keys.map((key) => `'${key}'`), CONJUNCTION_AND, CONJUNCTION_AND_COMMA);
|
|
62
|
+
}
|
|
63
|
+
function renderParts(parts, delimiterShort, delimiterLong) {
|
|
64
|
+
const { length } = parts;
|
|
65
|
+
if (length === 1) return parts[0];
|
|
66
|
+
let rendered = "";
|
|
67
|
+
for (let index = 0; index < length; index += 1) {
|
|
68
|
+
rendered += parts[index];
|
|
69
|
+
if (index < length - 2) rendered += ", ";
|
|
70
|
+
else if (index === length - 2) rendered += parts.length > 2 ? delimiterLong : delimiterShort;
|
|
71
|
+
}
|
|
72
|
+
return rendered;
|
|
73
|
+
}
|
|
74
|
+
function renderTypes(types) {
|
|
75
|
+
const unique = /* @__PURE__ */ new Set();
|
|
76
|
+
const parts = [];
|
|
77
|
+
for (let index = 0; index < types.length; index += 1) {
|
|
78
|
+
const rendered = getPropertyType(types[index]);
|
|
79
|
+
if (unique.has(rendered)) continue;
|
|
80
|
+
unique.add(rendered);
|
|
81
|
+
parts.push(rendered);
|
|
82
|
+
}
|
|
83
|
+
return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
|
|
84
|
+
}
|
|
85
|
+
function getRequiredMessage(key) {
|
|
86
|
+
return SCHEMATIC_MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED.replace("<>", key);
|
|
87
|
+
}
|
|
88
|
+
function getUnknownKeysMessage(keys) {
|
|
89
|
+
return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace("<>", renderKeys(keys));
|
|
90
|
+
}
|
|
91
|
+
//#endregion
|
|
92
|
+
export { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputPropertyValidatorMessage, getInputTypeMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage, getUnknownKeysMessage };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { ReportingInformation, ValidatorParameters } from "../models/validation.model.mjs";
|
|
2
|
+
import { Schematic } from "../schematic.mjs";
|
|
3
|
+
import { Constructor } from "@oscarpalmer/atoms/models";
|
|
4
|
+
|
|
5
|
+
//#region src/helpers/misc.helper.d.ts
|
|
6
|
+
declare function getParameters(input?: unknown): ValidatorParameters;
|
|
7
|
+
declare function getReporting(value?: unknown): ReportingInformation;
|
|
8
|
+
/**
|
|
9
|
+
* Creates a validator function for a given constructor
|
|
10
|
+
* @param constructor - Constructor to check against
|
|
11
|
+
* @throws Will throw a `TypeError` if the provided argument is not a valid constructor
|
|
12
|
+
* @returns Validator function that checks if a value is an instance of the constructor
|
|
13
|
+
*/
|
|
14
|
+
declare function instanceOf<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
|
|
15
|
+
/**
|
|
16
|
+
* Is the value a schematic?
|
|
17
|
+
* @param value Value to check
|
|
18
|
+
* @returns `true` if the value is a schematic, `false` otherwise
|
|
19
|
+
*/
|
|
20
|
+
declare function isSchematic(value: unknown): value is Schematic<never>;
|
|
21
|
+
//#endregion
|
|
22
|
+
export { getParameters, getReporting, instanceOf, isSchematic };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { MESSAGE_CONSTRUCTOR, REPORTING_FIRST, REPORTING_NONE, REPORTING_THROW, REPORTING_TYPES } from "../constants.mjs";
|
|
2
|
+
import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
|
+
//#region src/helpers/misc.helper.ts
|
|
4
|
+
function getParameters(input) {
|
|
5
|
+
if (typeof input === "boolean") return {
|
|
6
|
+
clone: true,
|
|
7
|
+
output: {},
|
|
8
|
+
reporting: getReporting(),
|
|
9
|
+
strict: input
|
|
10
|
+
};
|
|
11
|
+
if (REPORTING_TYPES.has(input)) return {
|
|
12
|
+
clone: true,
|
|
13
|
+
output: {},
|
|
14
|
+
reporting: getReporting(input),
|
|
15
|
+
strict: false
|
|
16
|
+
};
|
|
17
|
+
const options = isPlainObject(input) ? input : {};
|
|
18
|
+
return {
|
|
19
|
+
clone: typeof options.clone === "boolean" ? options.clone : true,
|
|
20
|
+
output: {},
|
|
21
|
+
reporting: getReporting(options.errors),
|
|
22
|
+
strict: typeof options.strict === "boolean" ? options.strict : false
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function getReporting(value) {
|
|
26
|
+
const type = REPORTING_TYPES.has(value) ? value : REPORTING_NONE;
|
|
27
|
+
return {
|
|
28
|
+
type,
|
|
29
|
+
["all"]: type === "all",
|
|
30
|
+
[REPORTING_FIRST]: type === REPORTING_FIRST,
|
|
31
|
+
[REPORTING_NONE]: type === REPORTING_NONE,
|
|
32
|
+
[REPORTING_THROW]: type === REPORTING_THROW
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Creates a validator function for a given constructor
|
|
37
|
+
* @param constructor - Constructor to check against
|
|
38
|
+
* @throws Will throw a `TypeError` if the provided argument is not a valid constructor
|
|
39
|
+
* @returns Validator function that checks if a value is an instance of the constructor
|
|
40
|
+
*/
|
|
41
|
+
function instanceOf(constructor) {
|
|
42
|
+
if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
|
|
43
|
+
return (value) => {
|
|
44
|
+
return value instanceof constructor;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Is the value a schematic?
|
|
49
|
+
* @param value Value to check
|
|
50
|
+
* @returns `true` if the value is a schematic, `false` otherwise
|
|
51
|
+
*/
|
|
52
|
+
function isSchematic(value) {
|
|
53
|
+
return typeof value === "object" && value !== null && "$schematic" in value && value["$schematic"] === true;
|
|
54
|
+
}
|
|
55
|
+
//#endregion
|
|
56
|
+
export { getParameters, getReporting, instanceOf, isSchematic };
|