@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
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import { CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA,
|
|
1
|
+
import { CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, 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, TYPES_ALL, TYPES_PREFIXED, TYPE_ARRAY, TYPE_FUNCTION_RESULT, TYPE_NULL, TYPE_OBJECT, 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 } from "../constants.mjs";
|
|
2
|
+
import { isValidator } from "./misc.helper.mjs";
|
|
3
|
+
import { validatorTypes } from "../validator.mjs";
|
|
2
4
|
import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
5
|
//#region src/helpers/message.helper.ts
|
|
4
6
|
function getDefaultRequiredMessage(key) {
|
|
@@ -23,15 +25,25 @@ function getInputPropertyMissingMessage(key, types) {
|
|
|
23
25
|
return message;
|
|
24
26
|
}
|
|
25
27
|
function getInputPropertyTypeMessage(key, types, actual) {
|
|
26
|
-
let message =
|
|
28
|
+
let message = VALIDATION_MESSAGE_INVALID_PROPERTY_TYPE.replace("<>", renderTypes(types));
|
|
27
29
|
message = message.replace("<>", key);
|
|
28
30
|
message = message.replace("<>", getValueType(actual));
|
|
29
31
|
return message;
|
|
30
32
|
}
|
|
31
33
|
function getInputPropertyValidatorMessage(key, type, index, length) {
|
|
32
|
-
let message =
|
|
34
|
+
let message = VALIDATION_MESSAGE_INVALID_PROPERTY_VALIDATOR.replace("<>", key);
|
|
33
35
|
message = message.replace("<>", type);
|
|
34
|
-
if (length > 1) message +=
|
|
36
|
+
if (length > 1) message += VALIDATION_MESSAGE_INVALID_VALIDATOR_SUFFIX.replace("<>", String(index));
|
|
37
|
+
return message;
|
|
38
|
+
}
|
|
39
|
+
function getInputValueTypeMessage(types, actual) {
|
|
40
|
+
let message = VALIDATION_MESSAGE_INVALID_VALUE_TYPE.replace("<>", renderTypes(types));
|
|
41
|
+
message = message.replace("<>", getValueType(actual));
|
|
42
|
+
return message;
|
|
43
|
+
}
|
|
44
|
+
function getInputValueValidatorMessage(type, index, length) {
|
|
45
|
+
let message = VALIDATION_MESSAGE_INVALID_VALUE_VALIDATOR.replace("<>", type);
|
|
46
|
+
if (length > 1) message += VALIDATION_MESSAGE_INVALID_VALIDATOR_SUFFIX.replace("<>", String(index));
|
|
35
47
|
return message;
|
|
36
48
|
}
|
|
37
49
|
function getSchematicPropertyNullableMessage(key) {
|
|
@@ -42,18 +54,19 @@ function getSchematicPropertyTypeMessage(key) {
|
|
|
42
54
|
}
|
|
43
55
|
function getPropertyType(type) {
|
|
44
56
|
switch (true) {
|
|
45
|
-
case typeof type === "function": return isConstructor(type) ? type.name : TYPE_FUNCTION_RESULT;
|
|
46
|
-
case
|
|
47
|
-
|
|
57
|
+
case typeof type === "function": return [isConstructor(type) ? type.name : TYPE_FUNCTION_RESULT];
|
|
58
|
+
case isValidator(type): return validatorTypes.get(type).flatMap(getPropertyType);
|
|
59
|
+
case TYPES_ALL.has(type): return [TYPES_PREFIXED[type]];
|
|
60
|
+
default: return [TYPES_PREFIXED[TYPE_OBJECT]];
|
|
48
61
|
}
|
|
49
62
|
}
|
|
50
63
|
function getValueType(value) {
|
|
51
64
|
const valueType = typeof value;
|
|
52
65
|
switch (true) {
|
|
53
66
|
case value === null: return TYPE_NULL;
|
|
54
|
-
case Array.isArray(value): return
|
|
55
|
-
case isPlainObject(value): return
|
|
56
|
-
case valueType !== TYPE_OBJECT: return
|
|
67
|
+
case Array.isArray(value): return TYPES_PREFIXED[TYPE_ARRAY];
|
|
68
|
+
case isPlainObject(value): return TYPES_PREFIXED[TYPE_OBJECT];
|
|
69
|
+
case valueType !== TYPE_OBJECT: return TYPES_PREFIXED[valueType];
|
|
57
70
|
default: return value.constructor.name;
|
|
58
71
|
}
|
|
59
72
|
}
|
|
@@ -74,11 +87,16 @@ function renderParts(parts, delimiterShort, delimiterLong) {
|
|
|
74
87
|
function renderTypes(types) {
|
|
75
88
|
const unique = /* @__PURE__ */ new Set();
|
|
76
89
|
const parts = [];
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
90
|
+
const typesLength = types.length;
|
|
91
|
+
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
92
|
+
const properties = getPropertyType(types[typeIndex]);
|
|
93
|
+
const propertiesLength = properties.length;
|
|
94
|
+
for (let propertyIndex = 0; propertyIndex < propertiesLength; propertyIndex += 1) {
|
|
95
|
+
const property = properties[propertyIndex];
|
|
96
|
+
if (unique.has(property)) continue;
|
|
97
|
+
unique.add(property);
|
|
98
|
+
parts.push(property);
|
|
99
|
+
}
|
|
82
100
|
}
|
|
83
101
|
return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
|
|
84
102
|
}
|
|
@@ -89,4 +107,4 @@ function getUnknownKeysMessage(keys) {
|
|
|
89
107
|
return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace("<>", renderKeys(keys));
|
|
90
108
|
}
|
|
91
109
|
//#endregion
|
|
92
|
-
export { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputPropertyValidatorMessage, getInputTypeMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage, getUnknownKeysMessage };
|
|
110
|
+
export { getDefaultRequiredMessage, getDefaultTypeMessage, getDisallowedMessage, getInputPropertyMissingMessage, getInputPropertyTypeMessage, getInputPropertyValidatorMessage, getInputTypeMessage, getInputValueTypeMessage, getInputValueValidatorMessage, getRequiredMessage, getSchematicPropertyNullableMessage, getSchematicPropertyTypeMessage, getUnknownKeysMessage };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { ReportingInformation,
|
|
1
|
+
import { ReportingInformation, ValidationHandlerParameters } from "../models/validation.model.mjs";
|
|
2
|
+
import { Validator } from "../validator.mjs";
|
|
2
3
|
import { Schema } from "../schema.mjs";
|
|
3
4
|
import { Constructor } from "@oscarpalmer/atoms/models";
|
|
4
5
|
|
|
5
6
|
//#region src/helpers/misc.helper.d.ts
|
|
6
|
-
declare function getParameters(input?: unknown):
|
|
7
|
+
declare function getParameters(input?: unknown): ValidationHandlerParameters;
|
|
7
8
|
declare function getReporting(value?: unknown): ReportingInformation;
|
|
8
9
|
/**
|
|
9
10
|
* Creates a validator function for a given constructor
|
|
@@ -13,10 +14,16 @@ declare function getReporting(value?: unknown): ReportingInformation;
|
|
|
13
14
|
*/
|
|
14
15
|
declare function instanceOf<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
|
|
15
16
|
/**
|
|
16
|
-
* Is the value a
|
|
17
|
+
* Is the value a schema?
|
|
17
18
|
* @param value Value to check
|
|
18
|
-
* @returns `true` if the value is a
|
|
19
|
+
* @returns `true` if the value is a schema, `false` otherwise
|
|
19
20
|
*/
|
|
20
|
-
declare function isSchema(value: unknown): value is Schema<
|
|
21
|
+
declare function isSchema(value: unknown): value is Schema<unknown>;
|
|
22
|
+
/**
|
|
23
|
+
* Is the value a validator?
|
|
24
|
+
* @param value Value to check
|
|
25
|
+
* @returns `true` if the value is a validator, `false` otherwise
|
|
26
|
+
*/
|
|
27
|
+
declare function isValidator(value: unknown): value is Validator<unknown>;
|
|
21
28
|
//#endregion
|
|
22
|
-
export { getParameters, getReporting, instanceOf, isSchema };
|
|
29
|
+
export { getParameters, getReporting, instanceOf, isSchema, isValidator };
|
|
@@ -27,7 +27,7 @@ function getReporting(value) {
|
|
|
27
27
|
return {
|
|
28
28
|
type,
|
|
29
29
|
["all"]: type === "all",
|
|
30
|
-
[REPORTING_FIRST]: type ===
|
|
30
|
+
[REPORTING_FIRST]: type === "first" || type === "result",
|
|
31
31
|
[REPORTING_NONE]: type === REPORTING_NONE,
|
|
32
32
|
[REPORTING_THROW]: type === REPORTING_THROW
|
|
33
33
|
};
|
|
@@ -45,12 +45,20 @@ function instanceOf(constructor) {
|
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
/**
|
|
48
|
-
* Is the value a
|
|
48
|
+
* Is the value a schema?
|
|
49
49
|
* @param value Value to check
|
|
50
|
-
* @returns `true` if the value is a
|
|
50
|
+
* @returns `true` if the value is a schema, `false` otherwise
|
|
51
51
|
*/
|
|
52
52
|
function isSchema(value) {
|
|
53
53
|
return typeof value === "object" && value !== null && "$schema" in value && value["$schema"] === true;
|
|
54
54
|
}
|
|
55
|
+
/**
|
|
56
|
+
* Is the value a validator?
|
|
57
|
+
* @param value Value to check
|
|
58
|
+
* @returns `true` if the value is a validator, `false` otherwise
|
|
59
|
+
*/
|
|
60
|
+
function isValidator(value) {
|
|
61
|
+
return typeof value === "object" && value !== null && "$validator" in value && value["$validator"] === true;
|
|
62
|
+
}
|
|
55
63
|
//#endregion
|
|
56
|
-
export { getParameters, getReporting, instanceOf, isSchema };
|
|
64
|
+
export { getParameters, getReporting, instanceOf, isSchema, isValidator };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { PropertyValidation, PropertyValidationKey, ValidationHandlerParameters } from "../models/validation.model.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers/report.helper.d.ts
|
|
4
|
+
type ReportParameters<Callback extends (...args: any[]) => string> = {
|
|
5
|
+
extract?: boolean;
|
|
6
|
+
information?: ReportParametersInformation;
|
|
7
|
+
key?: PropertyValidationKey;
|
|
8
|
+
message: ReportParametersMessage<Callback>;
|
|
9
|
+
original: ValidationHandlerParameters;
|
|
10
|
+
value: unknown;
|
|
11
|
+
};
|
|
12
|
+
type ReportParametersMessage<Callback extends (...args: any[]) => string> = {
|
|
13
|
+
arguments: Parameters<Callback>;
|
|
14
|
+
callback: Callback;
|
|
15
|
+
};
|
|
16
|
+
type ReportParametersInformation = {
|
|
17
|
+
all: PropertyValidation[];
|
|
18
|
+
existing?: PropertyValidation[];
|
|
19
|
+
};
|
|
20
|
+
declare function report<Callback extends (...args: any[]) => string>(parameters: ReportParameters<Callback>, getReports: true): PropertyValidation[];
|
|
21
|
+
declare function report<Callback extends (...args: any[]) => string>(parameters: ReportParameters<Callback>): PropertyValidation[] | undefined;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { report };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { ValidationError } from "../models/validation.model.mjs";
|
|
2
|
+
//#region src/helpers/report.helper.ts
|
|
3
|
+
function report(parameters, getReports) {
|
|
4
|
+
const { information, message, original } = parameters;
|
|
5
|
+
let reported;
|
|
6
|
+
if (information?.existing == null) {
|
|
7
|
+
reported = [{
|
|
8
|
+
value: parameters.value,
|
|
9
|
+
message: message.callback(...message.arguments)
|
|
10
|
+
}];
|
|
11
|
+
if (parameters.key != null) reported[0].key = parameters.key;
|
|
12
|
+
} else reported = information.existing;
|
|
13
|
+
if (original.reporting.throw) throw new ValidationError(reported);
|
|
14
|
+
information?.all.push(...reported);
|
|
15
|
+
if (parameters.extract ?? true) original.information?.push(...reported);
|
|
16
|
+
if ((getReports ?? false) || !original.reporting.all) return reported;
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { report };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidationHandler } from "../models/validation.model.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/helpers/result.helper.d.ts
|
|
4
|
+
declare function getResult(handler: ValidationHandler, value: unknown, options?: unknown): unknown;
|
|
5
|
+
declare function isResult(handler: ValidationHandler, value: unknown, options?: unknown): unknown;
|
|
6
|
+
//#endregion
|
|
7
|
+
export { getResult, isResult };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { getParameters } from "./misc.helper.mjs";
|
|
2
|
+
import { error, ok } from "@oscarpalmer/atoms/result/misc";
|
|
3
|
+
//#region src/helpers/result.helper.ts
|
|
4
|
+
function getResult(handler, value, options) {
|
|
5
|
+
const parameters = getParameters(options);
|
|
6
|
+
const result = handler(value, parameters, true);
|
|
7
|
+
if (result === true) return parameters.reporting.none || parameters.reporting.throw ? parameters.clone ? parameters.output : value : ok(parameters.clone ? parameters.output : value);
|
|
8
|
+
if (!parameters.reporting.none) return error(parameters.reporting.all ? result : result[0]);
|
|
9
|
+
}
|
|
10
|
+
function isResult(handler, value, options) {
|
|
11
|
+
const parameters = getParameters(options);
|
|
12
|
+
const result = handler(value, parameters, false);
|
|
13
|
+
if (result === true) return parameters.reporting.none || parameters.reporting.throw ? result : ok(result);
|
|
14
|
+
return parameters.reporting.none ? false : error(parameters.reporting.all ? result : result[0]);
|
|
15
|
+
}
|
|
16
|
+
//#endregion
|
|
17
|
+
export { getResult, isResult };
|