@oscarpalmer/jhunal 0.14.0 → 0.15.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/helpers.js CHANGED
@@ -1,11 +1,22 @@
1
1
  import { MESSAGE_CONSTRUCTOR } from "./constants.js";
2
2
  import { isConstructor } from "@oscarpalmer/atoms/is";
3
+ /**
4
+ * Creates a validator function for a given constructor
5
+ * @param constructor - Constructor to check against
6
+ * @throws Will throw a `TypeError` if the provided argument is not a valid constructor
7
+ * @returns Validator function that checks if a value is an instance of the constructor
8
+ */
3
9
  function instanceOf(constructor) {
4
10
  if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
5
11
  return (value) => {
6
12
  return value instanceof constructor;
7
13
  };
8
14
  }
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
+ */
9
20
  function isSchematic(value) {
10
21
  return typeof value === "object" && value !== null && "$schematic" in value && value["$schematic"] === true;
11
22
  }
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { instanceOf } from "./helpers.js";
1
+ import { instanceOf, isSchematic } from "./helpers.js";
2
2
  import { SchematicError } from "./models.js";
3
3
  import { schematic } from "./schematic.js";
4
- export { SchematicError, instanceOf, schematic };
4
+ export { SchematicError, instanceOf, isSchematic, schematic };
@@ -86,15 +86,34 @@ const TYPE_ALL = new Set([
86
86
  "null",
87
87
  TYPE_UNDEFINED
88
88
  ]);
89
+ /**
90
+ * Creates a validator function for a given constructor
91
+ * @param constructor - Constructor to check against
92
+ * @throws Will throw a `TypeError` if the provided argument is not a valid constructor
93
+ * @returns Validator function that checks if a value is an instance of the constructor
94
+ */
89
95
  function instanceOf(constructor) {
90
96
  if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
91
97
  return (value) => {
92
98
  return value instanceof constructor;
93
99
  };
94
100
  }
101
+ /**
102
+ * Is the value a schematic?
103
+ * @param value Value to check
104
+ * @returns `true` if the value is a schematic, `false` otherwise
105
+ */
95
106
  function isSchematic(value) {
96
107
  return typeof value === "object" && value !== null && SCHEMATIC_NAME in value && value[SCHEMATIC_NAME] === true;
97
108
  }
109
+ /**
110
+ * A custom error class for schema validation failures, with its `name` set to {@link ERROR_NAME}
111
+ *
112
+ * @example
113
+ * ```ts
114
+ * throw new SchematicError('Expected a string, received a number');
115
+ * ```
116
+ */
98
117
  var SchematicError = class extends Error {
99
118
  constructor(message) {
100
119
  super(message);
@@ -234,6 +253,8 @@ var Schematic = class {
234
253
  }
235
254
  /**
236
255
  * Does the value match the schema?
256
+ * @param value - Value to validate
257
+ * @returns `true` if the value matches the schema, otherwise `false`
237
258
  */
238
259
  is(value) {
239
260
  return validateObject(value, this.#properties);
@@ -244,4 +265,4 @@ function schematic(schema) {
244
265
  if (!isPlainObject(schema)) throw new SchematicError(MESSAGE_SCHEMA_INVALID_TYPE);
245
266
  return new Schematic(getProperties(schema));
246
267
  }
247
- export { SchematicError, instanceOf, schematic };
268
+ export { SchematicError, instanceOf, isSchematic, schematic };
package/dist/models.js CHANGED
@@ -1,4 +1,12 @@
1
1
  import { ERROR_NAME } from "./constants.js";
2
+ /**
3
+ * A custom error class for schema validation failures, with its `name` set to {@link ERROR_NAME}
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * throw new SchematicError('Expected a string, received a number');
8
+ * ```
9
+ */
2
10
  var SchematicError = class extends Error {
3
11
  constructor(message) {
4
12
  super(message);
package/dist/schematic.js CHANGED
@@ -15,6 +15,8 @@ var Schematic = class {
15
15
  }
16
16
  /**
17
17
  * Does the value match the schema?
18
+ * @param value - Value to validate
19
+ * @returns `true` if the value matches the schema, otherwise `false`
18
20
  */
19
21
  is(value) {
20
22
  return validateObject(value, this.#properties);
@@ -1,8 +1,8 @@
1
1
  import { EXPRESSION_PROPERTY, MESSAGE_SCHEMA_INVALID_EMPTY, MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED, MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE, MESSAGE_VALIDATOR_INVALID_KEY, MESSAGE_VALIDATOR_INVALID_TYPE, MESSAGE_VALIDATOR_INVALID_VALUE, PROPERTY_REQUIRED, PROPERTY_TYPE, PROPERTY_VALIDATORS, TEMPLATE_PATTERN_KEY, TEMPLATE_PATTERN_PROPERTY, TYPE_ALL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES } from "../constants.js";
2
2
  import { instanceOf, isSchematic } from "../helpers.js";
3
3
  import { SchematicError } from "../models.js";
4
- import { join } from "../node_modules/@oscarpalmer/atoms/dist/internal/string.js";
5
4
  import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
5
+ import { join } from "@oscarpalmer/atoms/string/misc";
6
6
  function getDisallowedProperty(obj) {
7
7
  if ("$required" in obj) return PROPERTY_REQUIRED;
8
8
  if ("$type" in obj) return PROPERTY_TYPE;
package/package.json CHANGED
@@ -45,5 +45,5 @@
45
45
  },
46
46
  "type": "module",
47
47
  "types": "./types/index.d.ts",
48
- "version": "0.14.0"
48
+ "version": "0.15.0"
49
49
  }
package/src/helpers.ts CHANGED
@@ -1,8 +1,14 @@
1
1
  import {isConstructor} from '@oscarpalmer/atoms/is';
2
+ import type {Constructor} from '@oscarpalmer/atoms/models';
2
3
  import {MESSAGE_CONSTRUCTOR, SCHEMATIC_NAME} from './constants';
3
- import type {Constructor} from './models';
4
4
  import type {Schematic} from './schematic';
5
5
 
6
+ /**
7
+ * Creates a validator function for a given constructor
8
+ * @param constructor - Constructor to check against
9
+ * @throws Will throw a `TypeError` if the provided argument is not a valid constructor
10
+ * @returns Validator function that checks if a value is an instance of the constructor
11
+ */
6
12
  export function instanceOf<Instance>(
7
13
  constructor: Constructor<Instance>,
8
14
  ): (value: unknown) => value is Instance {
@@ -15,6 +21,11 @@ export function instanceOf<Instance>(
15
21
  };
16
22
  }
17
23
 
24
+ /**
25
+ * Is the value a schematic?
26
+ * @param value Value to check
27
+ * @returns `true` if the value is a schematic, `false` otherwise
28
+ */
18
29
  export function isSchematic(value: unknown): value is Schematic<never> {
19
30
  return (
20
31
  typeof value === 'object' &&
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- export {instanceOf} from './helpers';
1
+ export {instanceOf, isSchematic} from './helpers';
2
2
  export {SchematicError, type Schema, type TypedSchema} from './models';
3
3
  export {schematic, type Schematic} from './schematic';