@oscarpalmer/jhunal 0.19.0 → 0.21.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.
@@ -2,6 +2,11 @@ import { ReportingType } from "./models/validation.model.mjs";
2
2
  import { Values } from "./models/misc.model.mjs";
3
3
 
4
4
  //#region src/constants.d.ts
5
+ declare const COMMA = ", ";
6
+ declare const CONJUNCTION_OR = " or ";
7
+ declare const CONJUNCTION_OR_COMMA = ", or ";
8
+ declare const CONJUNCTION_AND = " and ";
9
+ declare const CONJUNCTION_AND_COMMA = ", and ";
5
10
  declare const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
6
11
  declare const NAME_SCHEMATIC = "Schematic";
7
12
  declare const NAME_ERROR_SCHEMATIC = "SchematicError";
@@ -15,6 +20,7 @@ declare const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required pr
15
20
  declare const VALIDATION_MESSAGE_INVALID_TYPE = "Expected <> for '<>' but received <>";
16
21
  declare const VALIDATION_MESSAGE_INVALID_VALUE = "Value does not satisfy validator for '<>' and type '<>'";
17
22
  declare const VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX = " at index <>";
23
+ declare const VALIDATION_MESSAGE_UNKNOWN_KEYS = "Found keys that are not defined in the schema: <>";
18
24
  declare const REPORTING_ALL: ReportingType;
19
25
  declare const REPORTING_FIRST: ReportingType;
20
26
  declare const REPORTING_NONE: ReportingType;
@@ -37,4 +43,4 @@ declare const TYPE_UNDEFINED = "undefined";
37
43
  declare const VALIDATABLE_TYPES: Set<keyof Values>;
38
44
  declare const TYPE_ALL: Set<keyof Values>;
39
45
  //#endregion
40
- export { 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 };
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 };
@@ -1,4 +1,9 @@
1
1
  //#region src/constants.ts
2
+ const COMMA = ", ";
3
+ const CONJUNCTION_OR = " or ";
4
+ const CONJUNCTION_OR_COMMA = ", or ";
5
+ const CONJUNCTION_AND = " and ";
6
+ const CONJUNCTION_AND_COMMA = ", and ";
2
7
  const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
3
8
  const NAME_SCHEMATIC = "Schematic";
4
9
  const NAME_ERROR_SCHEMATIC = "SchematicError";
@@ -12,6 +17,7 @@ const VALIDATION_MESSAGE_INVALID_REQUIRED = "Expected <> for required property '
12
17
  const VALIDATION_MESSAGE_INVALID_TYPE = "Expected <> for '<>' but received <>";
13
18
  const VALIDATION_MESSAGE_INVALID_VALUE = "Value does not satisfy validator for '<>' and type '<>'";
14
19
  const VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX = " at index <>";
20
+ const VALIDATION_MESSAGE_UNKNOWN_KEYS = "Found keys that are not defined in the schema: <>";
15
21
  const REPORTING_ALL = "all";
16
22
  const REPORTING_FIRST = "first";
17
23
  const REPORTING_NONE = "none";
@@ -53,4 +59,4 @@ const TYPE_ALL = new Set([
53
59
  TYPE_UNDEFINED
54
60
  ]);
55
61
  //#endregion
56
- export { 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 };
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 };
@@ -1,14 +1,16 @@
1
- import { ReportingInformation, ValidatedProperty } from "./models/validation.model.mjs";
1
+ import { ReportingInformation, ValidatedPropertyType, ValidationParameters } from "./models/validation.model.mjs";
2
2
  import { Schematic } from "./schematic.mjs";
3
3
  import { ValueName } from "./models/misc.model.mjs";
4
4
  import { Constructor } from "@oscarpalmer/atoms/models";
5
5
 
6
6
  //#region src/helpers.d.ts
7
7
  declare function getInvalidInputMessage(actual: unknown): string;
8
- declare function getInvalidMissingMessage(property: ValidatedProperty): string;
9
- declare function getInvalidTypeMessage(property: ValidatedProperty, actual: unknown): string;
10
- declare function getInvalidValidatorMessage(property: ValidatedProperty, type: ValueName, index: number, length: number): string;
8
+ declare function getInvalidMissingMessage(key: string, types: ValidatedPropertyType[]): string;
9
+ declare function getInvalidTypeMessage(key: string, types: ValidatedPropertyType[], actual: unknown): string;
10
+ declare function getInvalidValidatorMessage(key: string, type: ValueName, index: number, length: number): string;
11
+ declare function getParameters(input?: unknown): ValidationParameters;
11
12
  declare function getReporting(value: unknown): ReportingInformation;
13
+ declare function getUnknownKeysMessage(keys: string[]): string;
12
14
  /**
13
15
  * Creates a validator function for a given constructor
14
16
  * @param constructor - Constructor to check against
@@ -23,4 +25,4 @@ declare function instanceOf<Instance>(constructor: Constructor<Instance>): (valu
23
25
  */
24
26
  declare function isSchematic(value: unknown): value is Schematic<never>;
25
27
  //#endregion
26
- export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getReporting, instanceOf, isSchematic };
28
+ export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getParameters, getReporting, getUnknownKeysMessage, instanceOf, isSchematic };
package/dist/helpers.mjs CHANGED
@@ -1,26 +1,44 @@
1
- import { 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 } from "./constants.mjs";
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
2
  import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
3
3
  //#region src/helpers.ts
4
4
  function getInvalidInputMessage(actual) {
5
5
  return VALIDATION_MESSAGE_INVALID_INPUT.replace("<>", getValueType(actual));
6
6
  }
7
- function getInvalidMissingMessage(property) {
8
- let message = VALIDATION_MESSAGE_INVALID_REQUIRED.replace("<>", renderTypes(property.types));
9
- message = message.replace("<>", property.key.full);
7
+ function getInvalidMissingMessage(key, types) {
8
+ let message = VALIDATION_MESSAGE_INVALID_REQUIRED.replace("<>", renderTypes(types));
9
+ message = message.replace("<>", key);
10
10
  return message;
11
11
  }
12
- function getInvalidTypeMessage(property, actual) {
13
- let message = VALIDATION_MESSAGE_INVALID_TYPE.replace("<>", renderTypes(property.types));
14
- message = message.replace("<>", property.key.full);
12
+ function getInvalidTypeMessage(key, types, actual) {
13
+ let message = VALIDATION_MESSAGE_INVALID_TYPE.replace("<>", renderTypes(types));
14
+ message = message.replace("<>", key);
15
15
  message = message.replace("<>", getValueType(actual));
16
16
  return message;
17
17
  }
18
- function getInvalidValidatorMessage(property, type, index, length) {
19
- let message = VALIDATION_MESSAGE_INVALID_VALUE.replace("<>", property.key.full);
18
+ function getInvalidValidatorMessage(key, type, index, length) {
19
+ let message = VALIDATION_MESSAGE_INVALID_VALUE.replace("<>", key);
20
20
  message = message.replace("<>", type);
21
21
  if (length > 1) message += VALIDATION_MESSAGE_INVALID_VALUE_SUFFIX.replace("<>", String(index));
22
22
  return message;
23
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
+ }
24
42
  function getPropertyType(original) {
25
43
  if (typeof original === "function") return "a validated value";
26
44
  if (Array.isArray(original)) return `'${TYPE_OBJECT}'`;
@@ -30,12 +48,16 @@ function getPropertyType(original) {
30
48
  function getReporting(value) {
31
49
  const type = REPORTING_TYPES.has(value) ? value : REPORTING_NONE;
32
50
  return {
51
+ type,
33
52
  ["all"]: type === "all",
34
53
  [REPORTING_FIRST]: type === REPORTING_FIRST,
35
54
  [REPORTING_NONE]: type === REPORTING_NONE,
36
55
  [REPORTING_THROW]: type === REPORTING_THROW
37
56
  };
38
57
  }
58
+ function getUnknownKeysMessage(keys) {
59
+ return VALIDATION_MESSAGE_UNKNOWN_KEYS.replace("<>", renderKeys(keys));
60
+ }
39
61
  function getValueType(value) {
40
62
  const valueType = typeof value;
41
63
  switch (true) {
@@ -68,6 +90,20 @@ function instanceOf(constructor) {
68
90
  function isSchematic(value) {
69
91
  return typeof value === "object" && value !== null && "$schematic" in value && value["$schematic"] === true;
70
92
  }
93
+ function renderKeys(keys) {
94
+ return renderParts(keys.map((key) => `'${key}'`), CONJUNCTION_AND, CONJUNCTION_AND_COMMA);
95
+ }
96
+ function renderParts(parts, delimiterShort, delimiterLong) {
97
+ const { length } = parts;
98
+ if (length === 1) return parts[0];
99
+ let rendered = "";
100
+ for (let index = 0; index < length; index += 1) {
101
+ rendered += parts[index];
102
+ if (index < length - 2) rendered += ", ";
103
+ else if (index === length - 2) rendered += parts.length > 2 ? delimiterLong : delimiterShort;
104
+ }
105
+ return rendered;
106
+ }
71
107
  function renderTypes(types) {
72
108
  const unique = /* @__PURE__ */ new Set();
73
109
  const parts = [];
@@ -77,14 +113,7 @@ function renderTypes(types) {
77
113
  unique.add(rendered);
78
114
  parts.push(rendered);
79
115
  }
80
- const { length } = parts;
81
- let rendered = "";
82
- for (let index = 0; index < length; index += 1) {
83
- rendered += parts[index];
84
- if (index < length - 2) rendered += ", ";
85
- else if (index === length - 2) rendered += parts.length > 2 ? ", or " : " or ";
86
- }
87
- return rendered;
116
+ return renderParts(parts, CONJUNCTION_OR, CONJUNCTION_OR_COMMA);
88
117
  }
89
118
  //#endregion
90
- export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getReporting, instanceOf, isSchematic };
119
+ export { getInvalidInputMessage, getInvalidMissingMessage, getInvalidTypeMessage, getInvalidValidatorMessage, getParameters, getReporting, getUnknownKeysMessage, instanceOf, isSchematic };