@oscarpalmer/jhunal 0.13.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/constants.js +5 -6
- package/dist/helpers.js +11 -0
- package/dist/index.js +2 -2
- package/dist/jhunal.full.js +42 -12
- package/dist/models.js +8 -0
- package/dist/schematic.js +2 -0
- package/dist/validation/property.validation.js +18 -10
- package/package.json +1 -1
- package/src/constants.ts +7 -9
- package/src/helpers.ts +12 -1
- package/src/index.ts +1 -1
- package/src/models.ts +399 -37
- package/src/schematic.ts +10 -0
- package/src/validation/property.validation.ts +37 -14
- package/types/constants.d.ts +4 -5
- package/types/helpers.d.ts +12 -1
- package/types/index.d.ts +1 -1
- package/types/models.d.ts +389 -12
- package/types/schematic.d.ts +10 -0
- package/types/validation/property.validation.d.ts +1 -1
- package/dist/node_modules/@oscarpalmer/atoms/dist/internal/array/compact.js +0 -12
- package/dist/node_modules/@oscarpalmer/atoms/dist/internal/string.js +0 -24
package/dist/constants.js
CHANGED
|
@@ -1,12 +1,9 @@
|
|
|
1
1
|
const ERROR_NAME = "SchematicError";
|
|
2
|
-
const EXPRESSION_INDEX = /\.\d+$/;
|
|
3
|
-
const EXPRESSION_KEY_PREFIX = /\.\w+$/;
|
|
4
|
-
const EXPRESSION_KEY_VALUE = /^.*\.(\w+)$/;
|
|
5
2
|
const EXPRESSION_PROPERTY = /(^|\.)\$(required|type|validators)(\.|$)/;
|
|
6
3
|
const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
|
|
7
4
|
const MESSAGE_SCHEMA_INVALID_EMPTY = "Schema must have at least one property";
|
|
8
|
-
const
|
|
9
|
-
const
|
|
5
|
+
const MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<key>.<property>' property is not allowed for schemas in $type";
|
|
6
|
+
const MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
|
|
10
7
|
const MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
11
8
|
const MESSAGE_SCHEMA_INVALID_TYPE = "Schema must be an object";
|
|
12
9
|
const MESSAGE_VALIDATOR_INVALID_KEY = "Validator '<>' does not exist";
|
|
@@ -17,6 +14,8 @@ const PROPERTY_TYPE = "$type";
|
|
|
17
14
|
const PROPERTY_VALIDATORS = "$validators";
|
|
18
15
|
const SCHEMATIC_NAME = "$schematic";
|
|
19
16
|
const TEMPLATE_PATTERN = "<>";
|
|
17
|
+
const TEMPLATE_PATTERN_KEY = "<key>";
|
|
18
|
+
const TEMPLATE_PATTERN_PROPERTY = "<property>";
|
|
20
19
|
const TYPE_OBJECT = "object";
|
|
21
20
|
const TYPE_UNDEFINED = "undefined";
|
|
22
21
|
const VALIDATABLE_TYPES = new Set([
|
|
@@ -35,4 +34,4 @@ const TYPE_ALL = new Set([
|
|
|
35
34
|
"null",
|
|
36
35
|
TYPE_UNDEFINED
|
|
37
36
|
]);
|
|
38
|
-
export { ERROR_NAME,
|
|
37
|
+
export { ERROR_NAME, EXPRESSION_PROPERTY, MESSAGE_CONSTRUCTOR, MESSAGE_SCHEMA_INVALID_EMPTY, MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED, MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE, MESSAGE_SCHEMA_INVALID_TYPE, MESSAGE_VALIDATOR_INVALID_KEY, MESSAGE_VALIDATOR_INVALID_TYPE, MESSAGE_VALIDATOR_INVALID_VALUE, PROPERTY_REQUIRED, PROPERTY_TYPE, PROPERTY_VALIDATORS, SCHEMATIC_NAME, TEMPLATE_PATTERN, TEMPLATE_PATTERN_KEY, TEMPLATE_PATTERN_PROPERTY, TYPE_ALL, TYPE_OBJECT, TYPE_UNDEFINED, VALIDATABLE_TYPES };
|
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 };
|
package/dist/jhunal.full.js
CHANGED
|
@@ -51,12 +51,11 @@ function join(value, delimiter) {
|
|
|
51
51
|
return compact(value).map(getString).join(typeof delimiter === "string" ? delimiter : "");
|
|
52
52
|
}
|
|
53
53
|
const ERROR_NAME = "SchematicError";
|
|
54
|
-
const EXPRESSION_INDEX = /\.\d+$/;
|
|
55
54
|
const EXPRESSION_PROPERTY = /(^|\.)\$(required|type|validators)(\.|$)/;
|
|
56
55
|
const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
|
|
57
56
|
const MESSAGE_SCHEMA_INVALID_EMPTY = "Schema must have at least one property";
|
|
58
|
-
const
|
|
59
|
-
const
|
|
57
|
+
const MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<key>.<property>' property is not allowed for schemas in $type";
|
|
58
|
+
const MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
|
|
60
59
|
const MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
61
60
|
const MESSAGE_SCHEMA_INVALID_TYPE = "Schema must be an object";
|
|
62
61
|
const MESSAGE_VALIDATOR_INVALID_KEY = "Validator '<>' does not exist";
|
|
@@ -67,6 +66,8 @@ const PROPERTY_TYPE = "$type";
|
|
|
67
66
|
const PROPERTY_VALIDATORS = "$validators";
|
|
68
67
|
const SCHEMATIC_NAME = "$schematic";
|
|
69
68
|
const TEMPLATE_PATTERN = "<>";
|
|
69
|
+
const TEMPLATE_PATTERN_KEY = "<key>";
|
|
70
|
+
const TEMPLATE_PATTERN_PROPERTY = "<property>";
|
|
70
71
|
const TYPE_OBJECT = "object";
|
|
71
72
|
const TYPE_UNDEFINED = "undefined";
|
|
72
73
|
const VALIDATABLE_TYPES = new Set([
|
|
@@ -85,30 +86,57 @@ const TYPE_ALL = new Set([
|
|
|
85
86
|
"null",
|
|
86
87
|
TYPE_UNDEFINED
|
|
87
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
|
+
*/
|
|
88
95
|
function instanceOf(constructor) {
|
|
89
96
|
if (!isConstructor(constructor)) throw new TypeError(MESSAGE_CONSTRUCTOR);
|
|
90
97
|
return (value) => {
|
|
91
98
|
return value instanceof constructor;
|
|
92
99
|
};
|
|
93
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
|
+
*/
|
|
94
106
|
function isSchematic(value) {
|
|
95
107
|
return typeof value === "object" && value !== null && SCHEMATIC_NAME in value && value[SCHEMATIC_NAME] === true;
|
|
96
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
|
+
*/
|
|
97
117
|
var SchematicError = class extends Error {
|
|
98
118
|
constructor(message) {
|
|
99
119
|
super(message);
|
|
100
120
|
this.name = ERROR_NAME;
|
|
101
121
|
}
|
|
102
122
|
};
|
|
103
|
-
function
|
|
123
|
+
function getDisallowedProperty(obj) {
|
|
124
|
+
if (PROPERTY_REQUIRED in obj) return PROPERTY_REQUIRED;
|
|
125
|
+
if (PROPERTY_TYPE in obj) return PROPERTY_TYPE;
|
|
126
|
+
if (PROPERTY_VALIDATORS in obj) return PROPERTY_VALIDATORS;
|
|
127
|
+
}
|
|
128
|
+
function getProperties(original, prefix, fromType) {
|
|
104
129
|
if (Object.keys(original).length === 0) throw new SchematicError(MESSAGE_SCHEMA_INVALID_EMPTY);
|
|
105
|
-
if (
|
|
130
|
+
if (fromType ?? false) {
|
|
131
|
+
const property = getDisallowedProperty(original);
|
|
132
|
+
if (property != null) throw new SchematicError(MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED.replace(TEMPLATE_PATTERN_KEY, prefix).replace(TEMPLATE_PATTERN_PROPERTY, property));
|
|
133
|
+
}
|
|
106
134
|
const keys = Object.keys(original);
|
|
107
135
|
const keysLength = keys.length;
|
|
108
136
|
const properties = [];
|
|
109
137
|
for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
110
138
|
const key = keys[keyIndex];
|
|
111
|
-
if (
|
|
139
|
+
if (EXPRESSION_PROPERTY.test(key)) continue;
|
|
112
140
|
const value = original[key];
|
|
113
141
|
const types = [];
|
|
114
142
|
let required = true;
|
|
@@ -116,8 +144,8 @@ function getProperties(original, prefix, fromTypes) {
|
|
|
116
144
|
if (isPlainObject(value)) {
|
|
117
145
|
required = getRequired(key, value) ?? required;
|
|
118
146
|
validators = getValidators(value[PROPERTY_VALIDATORS]);
|
|
119
|
-
if (PROPERTY_TYPE in value) types.push(
|
|
120
|
-
else types.push(
|
|
147
|
+
if (PROPERTY_TYPE in value) types.push(TYPE_OBJECT, ...getTypes(key, value[PROPERTY_TYPE], prefix, true));
|
|
148
|
+
else types.push(TYPE_OBJECT, ...getTypes(key, value, prefix));
|
|
121
149
|
} else types.push(...getTypes(key, value, prefix));
|
|
122
150
|
if (!required && !types.includes(TYPE_UNDEFINED)) types.push(TYPE_UNDEFINED);
|
|
123
151
|
properties.push({
|
|
@@ -131,10 +159,10 @@ function getProperties(original, prefix, fromTypes) {
|
|
|
131
159
|
}
|
|
132
160
|
function getRequired(key, obj) {
|
|
133
161
|
if (!(PROPERTY_REQUIRED in obj)) return;
|
|
134
|
-
if (typeof obj[PROPERTY_REQUIRED] !== "boolean") throw new SchematicError(
|
|
162
|
+
if (typeof obj[PROPERTY_REQUIRED] !== "boolean") throw new SchematicError(MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED.replace(TEMPLATE_PATTERN, key));
|
|
135
163
|
return obj[PROPERTY_REQUIRED];
|
|
136
164
|
}
|
|
137
|
-
function getTypes(key, original, prefix,
|
|
165
|
+
function getTypes(key, original, prefix, fromType) {
|
|
138
166
|
const array = Array.isArray(original) ? original : [original];
|
|
139
167
|
const { length } = array;
|
|
140
168
|
const types = [];
|
|
@@ -145,7 +173,7 @@ function getTypes(key, original, prefix, fromTypes) {
|
|
|
145
173
|
types.push(isConstructor(value) ? instanceOf(value) : value);
|
|
146
174
|
break;
|
|
147
175
|
case isPlainObject(value):
|
|
148
|
-
types.push(...getProperties(value, join([prefix, key], "."),
|
|
176
|
+
types.push(...getProperties(value, join([prefix, key], "."), fromType));
|
|
149
177
|
break;
|
|
150
178
|
case isSchematic(value):
|
|
151
179
|
types.push(value);
|
|
@@ -225,6 +253,8 @@ var Schematic = class {
|
|
|
225
253
|
}
|
|
226
254
|
/**
|
|
227
255
|
* Does the value match the schema?
|
|
256
|
+
* @param value - Value to validate
|
|
257
|
+
* @returns `true` if the value matches the schema, otherwise `false`
|
|
228
258
|
*/
|
|
229
259
|
is(value) {
|
|
230
260
|
return validateObject(value, this.#properties);
|
|
@@ -235,4 +265,4 @@ function schematic(schema) {
|
|
|
235
265
|
if (!isPlainObject(schema)) throw new SchematicError(MESSAGE_SCHEMA_INVALID_TYPE);
|
|
236
266
|
return new Schematic(getProperties(schema));
|
|
237
267
|
}
|
|
238
|
-
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
|
@@ -1,17 +1,25 @@
|
|
|
1
|
-
import {
|
|
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";
|
|
6
|
-
|
|
5
|
+
import { join } from "@oscarpalmer/atoms/string/misc";
|
|
6
|
+
function getDisallowedProperty(obj) {
|
|
7
|
+
if ("$required" in obj) return PROPERTY_REQUIRED;
|
|
8
|
+
if ("$type" in obj) return PROPERTY_TYPE;
|
|
9
|
+
if ("$validators" in obj) return PROPERTY_VALIDATORS;
|
|
10
|
+
}
|
|
11
|
+
function getProperties(original, prefix, fromType) {
|
|
7
12
|
if (Object.keys(original).length === 0) throw new SchematicError(MESSAGE_SCHEMA_INVALID_EMPTY);
|
|
8
|
-
if (
|
|
13
|
+
if (fromType ?? false) {
|
|
14
|
+
const property = getDisallowedProperty(original);
|
|
15
|
+
if (property != null) throw new SchematicError(MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED.replace(TEMPLATE_PATTERN_KEY, prefix).replace(TEMPLATE_PATTERN_PROPERTY, property));
|
|
16
|
+
}
|
|
9
17
|
const keys = Object.keys(original);
|
|
10
18
|
const keysLength = keys.length;
|
|
11
19
|
const properties = [];
|
|
12
20
|
for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
13
21
|
const key = keys[keyIndex];
|
|
14
|
-
if (
|
|
22
|
+
if (EXPRESSION_PROPERTY.test(key)) continue;
|
|
15
23
|
const value = original[key];
|
|
16
24
|
const types = [];
|
|
17
25
|
let required = true;
|
|
@@ -19,8 +27,8 @@ function getProperties(original, prefix, fromTypes) {
|
|
|
19
27
|
if (isPlainObject(value)) {
|
|
20
28
|
required = getRequired(key, value) ?? required;
|
|
21
29
|
validators = getValidators(value[PROPERTY_VALIDATORS]);
|
|
22
|
-
if ("$type" in value) types.push(
|
|
23
|
-
else types.push(
|
|
30
|
+
if ("$type" in value) types.push(TYPE_OBJECT, ...getTypes(key, value[PROPERTY_TYPE], prefix, true));
|
|
31
|
+
else types.push(TYPE_OBJECT, ...getTypes(key, value, prefix));
|
|
24
32
|
} else types.push(...getTypes(key, value, prefix));
|
|
25
33
|
if (!required && !types.includes("undefined")) types.push(TYPE_UNDEFINED);
|
|
26
34
|
properties.push({
|
|
@@ -34,10 +42,10 @@ function getProperties(original, prefix, fromTypes) {
|
|
|
34
42
|
}
|
|
35
43
|
function getRequired(key, obj) {
|
|
36
44
|
if (!("$required" in obj)) return;
|
|
37
|
-
if (typeof obj["$required"] !== "boolean") throw new SchematicError(
|
|
45
|
+
if (typeof obj["$required"] !== "boolean") throw new SchematicError(MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED.replace("<>", key));
|
|
38
46
|
return obj[PROPERTY_REQUIRED];
|
|
39
47
|
}
|
|
40
|
-
function getTypes(key, original, prefix,
|
|
48
|
+
function getTypes(key, original, prefix, fromType) {
|
|
41
49
|
const array = Array.isArray(original) ? original : [original];
|
|
42
50
|
const { length } = array;
|
|
43
51
|
const types = [];
|
|
@@ -48,7 +56,7 @@ function getTypes(key, original, prefix, fromTypes) {
|
|
|
48
56
|
types.push(isConstructor(value) ? instanceOf(value) : value);
|
|
49
57
|
break;
|
|
50
58
|
case isPlainObject(value):
|
|
51
|
-
types.push(...getProperties(value, join([prefix, key], "."),
|
|
59
|
+
types.push(...getProperties(value, join([prefix, key], "."), fromType));
|
|
52
60
|
break;
|
|
53
61
|
case isSchematic(value):
|
|
54
62
|
types.push(value);
|
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -2,22 +2,16 @@ import type {ValueName} from './models';
|
|
|
2
2
|
|
|
3
3
|
export const ERROR_NAME = 'SchematicError';
|
|
4
4
|
|
|
5
|
-
export const EXPRESSION_INDEX = /\.\d+$/;
|
|
6
|
-
|
|
7
|
-
export const EXPRESSION_KEY_PREFIX = /\.\w+$/;
|
|
8
|
-
|
|
9
|
-
export const EXPRESSION_KEY_VALUE = /^.*\.(\w+)$/;
|
|
10
|
-
|
|
11
5
|
export const EXPRESSION_PROPERTY = /(^|\.)\$(required|type|validators)(\.|$)/;
|
|
12
6
|
|
|
13
7
|
export const MESSAGE_CONSTRUCTOR = 'Expected a constructor function';
|
|
14
8
|
|
|
15
9
|
export const MESSAGE_SCHEMA_INVALID_EMPTY = 'Schema must have at least one property';
|
|
16
10
|
|
|
17
|
-
export const
|
|
18
|
-
"'
|
|
11
|
+
export const MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED =
|
|
12
|
+
"'<key>.<property>' property is not allowed for schemas in $type";
|
|
19
13
|
|
|
20
|
-
export const
|
|
14
|
+
export const MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED =
|
|
21
15
|
"'<>.$required' property must be a boolean";
|
|
22
16
|
|
|
23
17
|
export const MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
@@ -41,6 +35,10 @@ export const SCHEMATIC_NAME = '$schematic';
|
|
|
41
35
|
|
|
42
36
|
export const TEMPLATE_PATTERN = '<>';
|
|
43
37
|
|
|
38
|
+
export const TEMPLATE_PATTERN_KEY = '<key>';
|
|
39
|
+
|
|
40
|
+
export const TEMPLATE_PATTERN_PROPERTY = '<property>';
|
|
41
|
+
|
|
44
42
|
export const TYPE_OBJECT = 'object';
|
|
45
43
|
|
|
46
44
|
export const TYPE_UNDEFINED = 'undefined';
|
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