@oscarpalmer/jhunal 0.22.0 → 0.23.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 +7 -3
- package/dist/constants.mjs +34 -12
- package/dist/helpers/message.helper.d.mts +11 -0
- package/dist/helpers/message.helper.mjs +70 -0
- package/dist/helpers/misc.helper.d.mts +22 -0
- package/dist/helpers/misc.helper.mjs +56 -0
- package/dist/index.d.mts +304 -275
- package/dist/index.mjs +187 -154
- package/dist/models/validation.model.d.mts +18 -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 +22 -0
- package/dist/validator/named.validator.d.mts +7 -0
- package/dist/validator/named.validator.mjs +39 -0
- package/dist/validator/object.validator.d.mts +7 -0
- package/dist/{validation.mjs → validator/object.validator.mjs} +20 -98
- 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 +42 -10
- package/src/helpers/message.helper.ts +152 -0
- package/src/helpers/misc.helper.ts +93 -0
- package/src/index.ts +3 -3
- package/src/models/validation.model.ts +19 -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 +50 -0
- package/src/validator/named.validator.ts +62 -0
- package/src/{validation.ts → validator/object.validator.ts} +23 -181
- 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/src/helpers.ts +0 -249
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,14 @@ 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";
|
|
14
15
|
declare const PROPERTY_REQUIRED = "$required";
|
|
15
16
|
declare const PROPERTY_SCHEMATIC = "$schematic";
|
|
16
17
|
declare const PROPERTY_TYPE = "$type";
|
|
17
18
|
declare const PROPERTY_VALIDATORS = "$validators";
|
|
18
|
-
declare const VALIDATION_MESSAGE_INVALID_INPUT = "Expected
|
|
19
|
+
declare const VALIDATION_MESSAGE_INVALID_INPUT = "Expected an object as input but received <>";
|
|
19
20
|
declare const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '<>'";
|
|
20
21
|
declare const VALIDATION_MESSAGE_INVALID_TYPE = "Expected <> for '<>' but received <>";
|
|
21
22
|
declare const VALIDATION_MESSAGE_INVALID_VALUE = "Value does not satisfy validator for '<>' and type '<>'";
|
|
@@ -37,10 +38,13 @@ declare const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE = "Validators must be an
|
|
|
37
38
|
declare const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE = "Validator '<>' must be a function or an array of functions";
|
|
38
39
|
declare const TEMPLATE_PATTERN = "<>";
|
|
39
40
|
declare const TYPE_ARRAY = "array";
|
|
41
|
+
declare const TYPE_FUNCTION = "function";
|
|
42
|
+
declare const TYPE_FUNCTION_RESULT = "a validated value";
|
|
40
43
|
declare const TYPE_NULL = "null";
|
|
41
44
|
declare const TYPE_OBJECT = "object";
|
|
42
45
|
declare const TYPE_UNDEFINED = "undefined";
|
|
43
46
|
declare const VALIDATABLE_TYPES: Set<keyof Values>;
|
|
44
47
|
declare const TYPE_ALL: Set<keyof Values>;
|
|
48
|
+
declare const PREFIXED_TYPES: Record<ValueName, string>;
|
|
45
49
|
//#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 };
|
|
50
|
+
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_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_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,14 @@ 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";
|
|
11
12
|
const PROPERTY_REQUIRED = "$required";
|
|
12
13
|
const PROPERTY_SCHEMATIC = "$schematic";
|
|
13
14
|
const PROPERTY_TYPE = "$type";
|
|
14
15
|
const PROPERTY_VALIDATORS = "$validators";
|
|
15
|
-
const VALIDATION_MESSAGE_INVALID_INPUT = "Expected
|
|
16
|
+
const VALIDATION_MESSAGE_INVALID_INPUT = "Expected an object as input but received <>";
|
|
16
17
|
const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '<>'";
|
|
17
18
|
const VALIDATION_MESSAGE_INVALID_TYPE = "Expected <> for '<>' but received <>";
|
|
18
19
|
const VALIDATION_MESSAGE_INVALID_VALUE = "Value does not satisfy validator for '<>' and type '<>'";
|
|
@@ -39,24 +40,45 @@ const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_TYPE = "Validators must be an object";
|
|
|
39
40
|
const SCHEMATIC_MESSAGE_VALIDATOR_INVALID_VALUE = "Validator '<>' must be a function or an array of functions";
|
|
40
41
|
const TEMPLATE_PATTERN = "<>";
|
|
41
42
|
const TYPE_ARRAY = "array";
|
|
43
|
+
const TYPE_BIGINT = "bigint";
|
|
44
|
+
const TYPE_BOOLEAN = "boolean";
|
|
45
|
+
const TYPE_DATE = "date";
|
|
46
|
+
const TYPE_FUNCTION = "function";
|
|
47
|
+
const TYPE_FUNCTION_RESULT = "a validated value";
|
|
42
48
|
const TYPE_NULL = "null";
|
|
49
|
+
const TYPE_NUMBER = "number";
|
|
43
50
|
const TYPE_OBJECT = "object";
|
|
51
|
+
const TYPE_STRING = "string";
|
|
52
|
+
const TYPE_SYMBOL = "symbol";
|
|
44
53
|
const TYPE_UNDEFINED = "undefined";
|
|
45
54
|
const VALIDATABLE_TYPES = new Set([
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
TYPE_ARRAY,
|
|
56
|
+
TYPE_BIGINT,
|
|
57
|
+
TYPE_BOOLEAN,
|
|
58
|
+
TYPE_DATE,
|
|
59
|
+
TYPE_FUNCTION,
|
|
60
|
+
TYPE_NUMBER,
|
|
61
|
+
TYPE_OBJECT,
|
|
62
|
+
TYPE_STRING,
|
|
63
|
+
TYPE_SYMBOL
|
|
55
64
|
]);
|
|
56
65
|
const TYPE_ALL = new Set([
|
|
57
66
|
...VALIDATABLE_TYPES,
|
|
58
|
-
|
|
67
|
+
TYPE_NULL,
|
|
59
68
|
TYPE_UNDEFINED
|
|
60
69
|
]);
|
|
70
|
+
const PREFIXED_TYPES = {
|
|
71
|
+
[TYPE_ARRAY]: `an ${TYPE_ARRAY}`,
|
|
72
|
+
[TYPE_BIGINT]: `a ${TYPE_BIGINT}`,
|
|
73
|
+
[TYPE_BOOLEAN]: `a ${TYPE_BOOLEAN}`,
|
|
74
|
+
[TYPE_DATE]: `a ${TYPE_DATE}`,
|
|
75
|
+
[TYPE_FUNCTION]: `a ${TYPE_FUNCTION}`,
|
|
76
|
+
[TYPE_NULL]: TYPE_NULL,
|
|
77
|
+
[TYPE_NUMBER]: `a ${TYPE_NUMBER}`,
|
|
78
|
+
[TYPE_STRING]: `a ${TYPE_STRING}`,
|
|
79
|
+
[TYPE_SYMBOL]: `a ${TYPE_SYMBOL}`,
|
|
80
|
+
[TYPE_OBJECT]: `an ${TYPE_OBJECT}`,
|
|
81
|
+
[TYPE_UNDEFINED]: TYPE_UNDEFINED
|
|
82
|
+
};
|
|
61
83
|
//#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 };
|
|
84
|
+
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_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_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,11 @@
|
|
|
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 getInvalidInputMessage(actual: unknown): string;
|
|
6
|
+
declare function getInvalidMissingMessage(key: string, types: ValidatorType[]): string;
|
|
7
|
+
declare function getInvalidTypeMessage(key: string, types: ValidatorType[], actual: unknown): string;
|
|
8
|
+
declare function getInvalidValidatorMessage(key: string, type: ValueName, index: number, length: number): string;
|
|
9
|
+
declare function getUnknownKeysMessage(keys: string[]): string;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getUnknownKeysMessage };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, PREFIXED_TYPES, 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 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 getPropertyType(type) {
|
|
25
|
+
switch (true) {
|
|
26
|
+
case typeof type === "function": return isConstructor(type) ? type.name : TYPE_FUNCTION_RESULT;
|
|
27
|
+
case TYPE_ALL.has(type): return PREFIXED_TYPES[type];
|
|
28
|
+
default: return PREFIXED_TYPES[TYPE_OBJECT];
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
function getUnknownKeysMessage(keys) {
|
|
32
|
+
return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace("<>", renderKeys(keys));
|
|
33
|
+
}
|
|
34
|
+
function getValueType(value) {
|
|
35
|
+
const valueType = typeof value;
|
|
36
|
+
switch (true) {
|
|
37
|
+
case value === null: return TYPE_NULL;
|
|
38
|
+
case Array.isArray(value): return PREFIXED_TYPES[TYPE_ARRAY];
|
|
39
|
+
case isPlainObject(value): return PREFIXED_TYPES[TYPE_OBJECT];
|
|
40
|
+
case valueType !== TYPE_OBJECT: return PREFIXED_TYPES[valueType];
|
|
41
|
+
default: return value.constructor.name;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function renderKeys(keys) {
|
|
45
|
+
return renderParts(keys.map((key) => `'${key}'`), CONJUNCTION_AND, CONJUNCTION_AND_COMMA);
|
|
46
|
+
}
|
|
47
|
+
function renderParts(parts, delimiterShort, delimiterLong) {
|
|
48
|
+
const { length } = parts;
|
|
49
|
+
if (length === 1) return parts[0];
|
|
50
|
+
let rendered = "";
|
|
51
|
+
for (let index = 0; index < length; index += 1) {
|
|
52
|
+
rendered += parts[index];
|
|
53
|
+
if (index < length - 2) rendered += ", ";
|
|
54
|
+
else if (index === length - 2) rendered += parts.length > 2 ? delimiterLong : delimiterShort;
|
|
55
|
+
}
|
|
56
|
+
return rendered;
|
|
57
|
+
}
|
|
58
|
+
function renderTypes(types) {
|
|
59
|
+
const unique = /* @__PURE__ */ new Set();
|
|
60
|
+
const parts = [];
|
|
61
|
+
for (let index = 0; index < types.length; index += 1) {
|
|
62
|
+
const rendered = getPropertyType(types[index]);
|
|
63
|
+
if (unique.has(rendered)) continue;
|
|
64
|
+
unique.add(rendered);
|
|
65
|
+
parts.push(rendered);
|
|
66
|
+
}
|
|
67
|
+
return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
|
|
68
|
+
}
|
|
69
|
+
//#endregion
|
|
70
|
+
export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, 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(REPORTING_NONE),
|
|
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 };
|