@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.
Files changed (40) hide show
  1. package/dist/constants.d.mts +7 -3
  2. package/dist/constants.mjs +34 -12
  3. package/dist/helpers/message.helper.d.mts +11 -0
  4. package/dist/helpers/message.helper.mjs +70 -0
  5. package/dist/helpers/misc.helper.d.mts +22 -0
  6. package/dist/helpers/misc.helper.mjs +56 -0
  7. package/dist/index.d.mts +304 -275
  8. package/dist/index.mjs +187 -154
  9. package/dist/models/validation.model.d.mts +18 -7
  10. package/dist/schematic.d.mts +25 -7
  11. package/dist/schematic.mjs +6 -4
  12. package/dist/validator/base.validator.d.mts +6 -0
  13. package/dist/validator/base.validator.mjs +19 -0
  14. package/dist/validator/function.validator.d.mts +6 -0
  15. package/dist/validator/function.validator.mjs +9 -0
  16. package/dist/validator/named.handler.d.mts +6 -0
  17. package/dist/validator/named.handler.mjs +22 -0
  18. package/dist/validator/named.validator.d.mts +7 -0
  19. package/dist/validator/named.validator.mjs +39 -0
  20. package/dist/validator/object.validator.d.mts +7 -0
  21. package/dist/{validation.mjs → validator/object.validator.mjs} +20 -98
  22. package/dist/validator/schematic.validator.d.mts +7 -0
  23. package/dist/validator/schematic.validator.mjs +16 -0
  24. package/package.json +1 -1
  25. package/src/constants.ts +42 -10
  26. package/src/helpers/message.helper.ts +152 -0
  27. package/src/helpers/misc.helper.ts +93 -0
  28. package/src/index.ts +3 -3
  29. package/src/models/validation.model.ts +19 -6
  30. package/src/schematic.ts +43 -16
  31. package/src/validator/base.validator.ts +31 -0
  32. package/src/validator/function.validator.ts +9 -0
  33. package/src/validator/named.handler.ts +50 -0
  34. package/src/validator/named.validator.ts +62 -0
  35. package/src/{validation.ts → validator/object.validator.ts} +23 -181
  36. package/src/validator/schematic.validator.ts +25 -0
  37. package/dist/helpers.d.mts +0 -28
  38. package/dist/helpers.mjs +0 -120
  39. package/dist/validation.d.mts +0 -7
  40. package/src/helpers.ts +0 -249
@@ -1,28 +0,0 @@
1
- import { ReportingInformation, ValidatorParameters, ValidatorType } from "./models/validation.model.mjs";
2
- import { Schematic } from "./schematic.mjs";
3
- import { ValueName } from "./models/misc.model.mjs";
4
- import { Constructor } from "@oscarpalmer/atoms/models";
5
-
6
- //#region src/helpers.d.ts
7
- declare function getInvalidInputMessage(actual: unknown): string;
8
- declare function getInvalidMissingMessage(key: string, types: ValidatorType[]): string;
9
- declare function getInvalidTypeMessage(key: string, types: ValidatorType[], actual: unknown): string;
10
- declare function getInvalidValidatorMessage(key: string, type: ValueName, index: number, length: number): string;
11
- declare function getParameters(input?: unknown): ValidatorParameters;
12
- declare function getReporting(value: unknown): ReportingInformation;
13
- declare function getUnknownKeysMessage(keys: string[]): string;
14
- /**
15
- * Creates a validator function for a given constructor
16
- * @param constructor - Constructor to check against
17
- * @throws Will throw a `TypeError` if the provided argument is not a valid constructor
18
- * @returns Validator function that checks if a value is an instance of the constructor
19
- */
20
- declare function instanceOf<Instance>(constructor: Constructor<Instance>): (value: unknown) => value is Instance;
21
- /**
22
- * Is the value a schematic?
23
- * @param value Value to check
24
- * @returns `true` if the value is a schematic, `false` otherwise
25
- */
26
- declare function isSchematic(value: unknown): value is Schematic<never>;
27
- //#endregion
28
- export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getParameters, getReporting, getUnknownKeysMessage, instanceOf, isSchematic };
package/dist/helpers.mjs DELETED
@@ -1,120 +0,0 @@
1
- import { CONJUNCTION_AND, CONJUNCTION_AND_COMMA, CONJUNCTION_OR, CONJUNCTION_OR_COMMA, MESSAGE_CONSTRUCTOR, NAME_SCHEMATIC, REPORTING_FIRST, REPORTING_NONE, REPORTING_THROW, REPORTING_TYPES, TYPE_ARRAY, TYPE_NULL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATION_MESSAGE_INVALID_INPUT, VALIDATION_MESSAGE_INVALID_REQUIRED, VALIDATION_MESSAGE_INVALID_TYPE, VALIDATION_MESSAGE_INVALID_VALUE, VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX, VALIDATION_MESSAGE_UNKNOWN_KEYS } from "./constants.mjs";
2
- import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
3
- //#region src/helpers.ts
4
- function getInvalidInputMessage(actual) {
5
- return VALIDATION_MESSAGE_INVALID_INPUT.replace("<>", getValueType(actual));
6
- }
7
- function getInvalidMissingMessage(key, types) {
8
- let message = VALIDATION_MESSAGE_INVALID_REQUIRED.replace("<>", renderTypes(types));
9
- message = message.replace("<>", key);
10
- return message;
11
- }
12
- function getInvalidTypeMessage(key, types, actual) {
13
- let message = VALIDATION_MESSAGE_INVALID_TYPE.replace("<>", renderTypes(types));
14
- message = message.replace("<>", key);
15
- message = message.replace("<>", getValueType(actual));
16
- return message;
17
- }
18
- function getInvalidValidatorMessage(key, type, index, length) {
19
- let message = VALIDATION_MESSAGE_INVALID_VALUE.replace("<>", key);
20
- message = message.replace("<>", type);
21
- if (length > 1) message += VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX.replace("<>", String(index));
22
- return message;
23
- }
24
- function getParameters(input) {
25
- if (typeof input === "boolean") return {
26
- output: {},
27
- reporting: getReporting(REPORTING_NONE),
28
- strict: input
29
- };
30
- if (REPORTING_TYPES.has(input)) return {
31
- output: {},
32
- reporting: getReporting(input),
33
- strict: false
34
- };
35
- const options = isPlainObject(input) ? input : {};
36
- return {
37
- output: {},
38
- reporting: getReporting(options.errors),
39
- strict: typeof options.strict === "boolean" ? options.strict : false
40
- };
41
- }
42
- function getPropertyType(original) {
43
- if (typeof original === "function") return "a validated value";
44
- if (Array.isArray(original)) return `'array'`;
45
- if (isPlainObject(original)) return `'${TYPE_OBJECT}'`;
46
- if (isSchematic(original)) return `a ${NAME_SCHEMATIC}`;
47
- return `'${String(original)}'`;
48
- }
49
- function getReporting(value) {
50
- const type = REPORTING_TYPES.has(value) ? value : REPORTING_NONE;
51
- return {
52
- type,
53
- ["all"]: type === "all",
54
- [REPORTING_FIRST]: type === REPORTING_FIRST,
55
- [REPORTING_NONE]: type === REPORTING_NONE,
56
- [REPORTING_THROW]: type === REPORTING_THROW
57
- };
58
- }
59
- function getUnknownKeysMessage(keys) {
60
- return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace("<>", renderKeys(keys));
61
- }
62
- function getValueType(value) {
63
- const valueType = typeof value;
64
- switch (true) {
65
- case value === null: return `'${TYPE_NULL}'`;
66
- case value === void 0: return `'${TYPE_UNDEFINED}'`;
67
- case valueType !== TYPE_OBJECT: return `'${valueType}'`;
68
- case Array.isArray(value): return `'${TYPE_ARRAY}'`;
69
- case isPlainObject(value): return `'${TYPE_OBJECT}'`;
70
- case isSchematic(value): return `a ${NAME_SCHEMATIC}`;
71
- default: return value.constructor.name;
72
- }
73
- }
74
- /**
75
- * Creates a validator function for a given constructor
76
- * @param constructor - Constructor to check against
77
- * @throws Will throw a `TypeError` if the provided argument is not a valid constructor
78
- * @returns Validator function that checks if a value is an instance of the constructor
79
- */
80
- function instanceOf(constructor) {
81
- if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
82
- return (value) => {
83
- return value instanceof constructor;
84
- };
85
- }
86
- /**
87
- * Is the value a schematic?
88
- * @param value Value to check
89
- * @returns `true` if the value is a schematic, `false` otherwise
90
- */
91
- function isSchematic(value) {
92
- return typeof value === "object" && value !== null && "$schematic" in value && value["$schematic"] === true;
93
- }
94
- function renderKeys(keys) {
95
- return renderParts(keys.map((key) => `'${key}'`), CONJUNCTION_AND, CONJUNCTION_AND_COMMA);
96
- }
97
- function renderParts(parts, delimiterShort, delimiterLong) {
98
- const { length } = parts;
99
- if (length === 1) return parts[0];
100
- let rendered = "";
101
- for (let index = 0; index < length; index += 1) {
102
- rendered += parts[index];
103
- if (index < length - 2) rendered += ", ";
104
- else if (index === length - 2) rendered += parts.length > 2 ? delimiterLong : delimiterShort;
105
- }
106
- return rendered;
107
- }
108
- function renderTypes(types) {
109
- const unique = /* @__PURE__ */ new Set();
110
- const parts = [];
111
- for (let index = 0; index < types.length; index += 1) {
112
- const rendered = getPropertyType(types[index]);
113
- if (unique.has(rendered)) continue;
114
- unique.add(rendered);
115
- parts.push(rendered);
116
- }
117
- return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
118
- }
119
- //#endregion
120
- export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getParameters, getReporting, getUnknownKeysMessage, instanceOf, isSchematic };
@@ -1,7 +0,0 @@
1
- import { ValidationInformationKey, Validator } from "./models/validation.model.mjs";
2
- import { PlainObject } from "@oscarpalmer/atoms";
3
-
4
- //#region src/validation.d.ts
5
- declare function getObjectValidator(original: PlainObject, origin?: ValidationInformationKey, fromType?: boolean): Validator;
6
- //#endregion
7
- export { getObjectValidator };
package/src/helpers.ts DELETED
@@ -1,249 +0,0 @@
1
- import {isConstructor, isPlainObject} from '@oscarpalmer/atoms/is';
2
- import type {Constructor} from '@oscarpalmer/atoms/models';
3
- import {
4
- COMMA,
5
- CONJUNCTION_AND,
6
- CONJUNCTION_AND_COMMA,
7
- CONJUNCTION_OR,
8
- CONJUNCTION_OR_COMMA,
9
- MESSAGE_CONSTRUCTOR,
10
- NAME_SCHEMATIC,
11
- PROPERTY_SCHEMATIC,
12
- REPORTING_ALL,
13
- REPORTING_FIRST,
14
- REPORTING_NONE,
15
- REPORTING_THROW,
16
- REPORTING_TYPES,
17
- TEMPLATE_PATTERN,
18
- TYPE_ARRAY,
19
- TYPE_NULL,
20
- TYPE_OBJECT,
21
- TYPE_UNDEFINED,
22
- VALIDATION_MESSAGE_INVALID_INPUT,
23
- VALIDATION_MESSAGE_INVALID_REQUIRED,
24
- VALIDATION_MESSAGE_INVALID_TYPE,
25
- VALIDATION_MESSAGE_INVALID_VALUE,
26
- VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX,
27
- VALIDATION_MESSAGE_UNKNOWN_KEYS,
28
- } from './constants';
29
- import type {ValueName} from './models/misc.model';
30
- import type {
31
- ReportingInformation,
32
- ReportingType,
33
- ValidatorParameters,
34
- ValidatorType,
35
- } from './models/validation.model';
36
- import type {Schematic} from './schematic';
37
-
38
- export function getInvalidInputMessage(actual: unknown): string {
39
- return VALIDATION_MESSAGE_INVALID_INPUT.replace(TEMPLATE_PATTERN, getValueType(actual));
40
- }
41
-
42
- export function getInvalidMissingMessage(key: string, types: ValidatorType[]): string {
43
- let message = VALIDATION_MESSAGE_INVALID_REQUIRED.replace(TEMPLATE_PATTERN, renderTypes(types));
44
-
45
- message = message.replace(TEMPLATE_PATTERN, key);
46
-
47
- return message;
48
- }
49
-
50
- export function getInvalidTypeMessage(
51
- key: string,
52
- types: ValidatorType[],
53
- actual: unknown,
54
- ): string {
55
- let message = VALIDATION_MESSAGE_INVALID_TYPE.replace(TEMPLATE_PATTERN, renderTypes(types));
56
-
57
- message = message.replace(TEMPLATE_PATTERN, key);
58
- message = message.replace(TEMPLATE_PATTERN, getValueType(actual));
59
-
60
- return message;
61
- }
62
-
63
- export function getInvalidValidatorMessage(
64
- key: string,
65
- type: ValueName,
66
- index: number,
67
- length: number,
68
- ): string {
69
- let message = VALIDATION_MESSAGE_INVALID_VALUE.replace(TEMPLATE_PATTERN, key);
70
-
71
- message = message.replace(TEMPLATE_PATTERN, type);
72
-
73
- if (length > 1) {
74
- message += VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX.replace(TEMPLATE_PATTERN, String(index));
75
- }
76
-
77
- return message;
78
- }
79
-
80
- export function getParameters(input?: unknown): ValidatorParameters {
81
- if (typeof input === 'boolean') {
82
- return {
83
- output: {},
84
- reporting: getReporting(REPORTING_NONE),
85
- strict: input,
86
- };
87
- }
88
-
89
- if (REPORTING_TYPES.has(input as ReportingType)) {
90
- return {
91
- output: {},
92
- reporting: getReporting(input as ReportingType),
93
- strict: false,
94
- };
95
- }
96
-
97
- const options = isPlainObject(input) ? input : {};
98
-
99
- return {
100
- output: {},
101
- reporting: getReporting(options.errors),
102
- strict: typeof options.strict === 'boolean' ? options.strict : false,
103
- };
104
- }
105
-
106
- function getPropertyType(original: ValidatorType): string {
107
- if (typeof original === 'function') {
108
- return 'a validated value';
109
- }
110
-
111
- if (Array.isArray(original)) {
112
- return `'array'`;
113
- }
114
-
115
- if (isPlainObject(original)) {
116
- return `'${TYPE_OBJECT}'`;
117
- }
118
-
119
- if (isSchematic(original)) {
120
- return `a ${NAME_SCHEMATIC}`;
121
- }
122
-
123
- return `'${String(original)}'`;
124
- }
125
-
126
- export function getReporting(value: unknown): ReportingInformation {
127
- const type = REPORTING_TYPES.has(value as ReportingType)
128
- ? (value as ReportingType)
129
- : REPORTING_NONE;
130
-
131
- return {
132
- type,
133
- [REPORTING_ALL]: type === REPORTING_ALL,
134
- [REPORTING_FIRST]: type === REPORTING_FIRST,
135
- [REPORTING_NONE]: type === REPORTING_NONE,
136
- [REPORTING_THROW]: type === REPORTING_THROW,
137
- } as ReportingInformation;
138
- }
139
-
140
- export function getUnknownKeysMessage(keys: string[]): string {
141
- return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace(TEMPLATE_PATTERN, renderKeys(keys));
142
- }
143
-
144
- function getValueType(value: unknown): string {
145
- const valueType = typeof value;
146
-
147
- switch (true) {
148
- case value === null:
149
- return `'${TYPE_NULL}'`;
150
-
151
- case value === undefined:
152
- return `'${TYPE_UNDEFINED}'`;
153
-
154
- case valueType !== TYPE_OBJECT:
155
- return `'${valueType}'`;
156
-
157
- case Array.isArray(value):
158
- return `'${TYPE_ARRAY}'`;
159
-
160
- case isPlainObject(value):
161
- return `'${TYPE_OBJECT}'`;
162
-
163
- case isSchematic(value):
164
- return `a ${NAME_SCHEMATIC}`;
165
-
166
- default:
167
- return value.constructor.name;
168
- }
169
- }
170
-
171
- /**
172
- * Creates a validator function for a given constructor
173
- * @param constructor - Constructor to check against
174
- * @throws Will throw a `TypeError` if the provided argument is not a valid constructor
175
- * @returns Validator function that checks if a value is an instance of the constructor
176
- */
177
- export function instanceOf<Instance>(
178
- constructor: Constructor<Instance>,
179
- ): (value: unknown) => value is Instance {
180
- if (!isConstructor(constructor)) {
181
- throw new TypeError(MESSAGE_CONSTRUCTOR);
182
- }
183
-
184
- return (value: unknown): value is Instance => {
185
- return value instanceof constructor;
186
- };
187
- }
188
-
189
- /**
190
- * Is the value a schematic?
191
- * @param value Value to check
192
- * @returns `true` if the value is a schematic, `false` otherwise
193
- */
194
- export function isSchematic(value: unknown): value is Schematic<never> {
195
- return (
196
- typeof value === 'object' &&
197
- value !== null &&
198
- PROPERTY_SCHEMATIC in value &&
199
- value[PROPERTY_SCHEMATIC] === true
200
- );
201
- }
202
-
203
- function renderKeys(keys: string[]): string {
204
- return renderParts(
205
- keys.map(key => `'${key}'`),
206
- CONJUNCTION_AND,
207
- CONJUNCTION_AND_COMMA,
208
- );
209
- }
210
-
211
- function renderParts(parts: string[], delimiterShort: string, delimiterLong: string): string {
212
- const {length} = parts;
213
-
214
- if (length === 1) {
215
- return parts[0];
216
- }
217
-
218
- let rendered = '';
219
-
220
- for (let index = 0; index < length; index += 1) {
221
- rendered += parts[index];
222
-
223
- if (index < length - 2) {
224
- rendered += COMMA;
225
- } else if (index === length - 2) {
226
- rendered += parts.length > 2 ? delimiterLong : delimiterShort;
227
- }
228
- }
229
-
230
- return rendered;
231
- }
232
-
233
- function renderTypes(types: ValidatorType[]): string {
234
- const unique = new Set<string>();
235
- const parts: string[] = [];
236
-
237
- for (let index = 0; index < types.length; index += 1) {
238
- const rendered = getPropertyType(types[index]);
239
-
240
- if (unique.has(rendered)) {
241
- continue;
242
- }
243
-
244
- unique.add(rendered);
245
- parts.push(rendered);
246
- }
247
-
248
- return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
249
- }