@oscarpalmer/jhunal 0.15.0 → 0.17.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 +27 -0
- package/dist/{constants.js → constants.mjs} +4 -2
- package/{types/helpers.d.ts → dist/helpers.d.mts} +8 -4
- package/dist/{helpers.js → helpers.mjs} +3 -1
- package/dist/index.d.mts +530 -0
- package/dist/{jhunal.full.js → index.mjs} +31 -75
- package/{types/models.d.ts → dist/models.d.mts} +104 -152
- package/dist/models.mjs +13 -0
- package/dist/schematic.d.mts +36 -0
- package/dist/{schematic.js → schematic.mjs} +8 -6
- package/dist/validation/property.validation.d.mts +7 -0
- package/dist/validation/{property.validation.js → property.validation.mjs} +7 -5
- package/dist/validation/value.validation.d.mts +6 -0
- package/dist/validation/{value.validation.js → value.validation.mjs} +4 -2
- package/package.json +52 -47
- package/src/constants.ts +4 -4
- package/src/models.ts +14 -40
- package/src/schematic.ts +5 -5
- package/src/validation/property.validation.ts +11 -6
- package/dist/index.js +0 -4
- package/dist/models.js +0 -16
- package/types/constants.d.ts +0 -22
- package/types/index.d.ts +0 -3
- package/types/schematic.d.ts +0 -32
- package/types/validation/property.validation.d.ts +0 -3
- package/types/validation/value.validation.d.ts +0 -2
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Infer, Schema, TypedSchema, ValidatedProperty } from "./models.mjs";
|
|
2
|
+
import { PlainObject } from "@oscarpalmer/atoms/models";
|
|
3
|
+
|
|
4
|
+
//#region src/schematic.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* A schematic for validating objects
|
|
7
|
+
*/
|
|
8
|
+
declare class Schematic<Model> {
|
|
9
|
+
#private;
|
|
10
|
+
private readonly $schematic;
|
|
11
|
+
constructor(properties: ValidatedProperty[]);
|
|
12
|
+
/**
|
|
13
|
+
* Does the value match the schema?
|
|
14
|
+
* @param value Value to validate
|
|
15
|
+
* @returns `true` if the value matches the schema, otherwise `false`
|
|
16
|
+
*/
|
|
17
|
+
is(value: unknown): value is Model;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a schematic from a schema
|
|
21
|
+
* @template Model Schema type
|
|
22
|
+
* @param schema Schema to create the schematic from
|
|
23
|
+
* @throws Throws {@link SchematicError} if the schema can not be converted into a schematic
|
|
24
|
+
* @returns A schematic for the given schema
|
|
25
|
+
*/
|
|
26
|
+
declare function schematic<Model extends Schema>(schema: Model): Schematic<Infer<Model>>;
|
|
27
|
+
/**
|
|
28
|
+
* Create a schematic from a typed schema
|
|
29
|
+
* @template Model Existing type
|
|
30
|
+
* @param schema Typed schema to create the schematic from
|
|
31
|
+
* @throws Throws {@link SchematicError} if the schema can not be converted into a schematic
|
|
32
|
+
* @returns A schematic for the given typed schema
|
|
33
|
+
*/
|
|
34
|
+
declare function schematic<Model extends PlainObject>(schema: TypedSchema<Model>): Schematic<Model>;
|
|
35
|
+
//#endregion
|
|
36
|
+
export { Schematic, schematic };
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { MESSAGE_SCHEMA_INVALID_TYPE, SCHEMATIC_NAME } from "./constants.
|
|
2
|
-
import { isSchematic } from "./helpers.
|
|
3
|
-
import { SchematicError } from "./models.
|
|
4
|
-
import { getProperties } from "./validation/property.validation.
|
|
5
|
-
import { validateObject } from "./validation/value.validation.
|
|
1
|
+
import { MESSAGE_SCHEMA_INVALID_TYPE, SCHEMATIC_NAME } from "./constants.mjs";
|
|
2
|
+
import { isSchematic } from "./helpers.mjs";
|
|
3
|
+
import { SchematicError } from "./models.mjs";
|
|
4
|
+
import { getProperties } from "./validation/property.validation.mjs";
|
|
5
|
+
import { validateObject } from "./validation/value.validation.mjs";
|
|
6
6
|
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
7
|
+
//#region src/schematic.ts
|
|
7
8
|
/**
|
|
8
9
|
* A schematic for validating objects
|
|
9
10
|
*/
|
|
@@ -15,7 +16,7 @@ var Schematic = class {
|
|
|
15
16
|
}
|
|
16
17
|
/**
|
|
17
18
|
* Does the value match the schema?
|
|
18
|
-
* @param value
|
|
19
|
+
* @param value Value to validate
|
|
19
20
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
20
21
|
*/
|
|
21
22
|
is(value) {
|
|
@@ -27,4 +28,5 @@ function schematic(schema) {
|
|
|
27
28
|
if (!isPlainObject(schema)) throw new SchematicError(MESSAGE_SCHEMA_INVALID_TYPE);
|
|
28
29
|
return new Schematic(getProperties(schema));
|
|
29
30
|
}
|
|
31
|
+
//#endregion
|
|
30
32
|
export { Schematic, schematic };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { ValidatedProperty } from "../models.mjs";
|
|
2
|
+
import { PlainObject } from "@oscarpalmer/atoms/models";
|
|
3
|
+
|
|
4
|
+
//#region src/validation/property.validation.d.ts
|
|
5
|
+
declare function getProperties(original: PlainObject, prefix?: string, fromType?: boolean): ValidatedProperty[];
|
|
6
|
+
//#endregion
|
|
7
|
+
export { getProperties };
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { instanceOf, isSchematic } from "../helpers.
|
|
3
|
-
import { SchematicError } from "../models.
|
|
1
|
+
import { MESSAGE_SCHEMA_INVALID_EMPTY, MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED, MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE, 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.mjs";
|
|
2
|
+
import { instanceOf, isSchematic } from "../helpers.mjs";
|
|
3
|
+
import { SchematicError } from "../models.mjs";
|
|
4
4
|
import { isConstructor, isPlainObject } from "@oscarpalmer/atoms/is";
|
|
5
|
-
import { join } from "@oscarpalmer/atoms/string
|
|
5
|
+
import { join } from "@oscarpalmer/atoms/string";
|
|
6
|
+
//#region src/validation/property.validation.ts
|
|
6
7
|
function getDisallowedProperty(obj) {
|
|
7
8
|
if ("$required" in obj) return PROPERTY_REQUIRED;
|
|
8
9
|
if ("$type" in obj) return PROPERTY_TYPE;
|
|
@@ -19,8 +20,8 @@ function getProperties(original, prefix, fromType) {
|
|
|
19
20
|
const properties = [];
|
|
20
21
|
for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
21
22
|
const key = keys[keyIndex];
|
|
22
|
-
if (EXPRESSION_PROPERTY.test(key)) continue;
|
|
23
23
|
const value = original[key];
|
|
24
|
+
if (value == null) throw new SchematicError(MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE.replace("<>", join([prefix, key], ".")));
|
|
24
25
|
const types = [];
|
|
25
26
|
let required = true;
|
|
26
27
|
let validators = {};
|
|
@@ -87,4 +88,5 @@ function getValidators(original) {
|
|
|
87
88
|
}
|
|
88
89
|
return validators;
|
|
89
90
|
}
|
|
91
|
+
//#endregion
|
|
90
92
|
export { getProperties };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { isSchematic } from "../helpers.
|
|
1
|
+
import { isSchematic } from "../helpers.mjs";
|
|
2
2
|
import { isPlainObject } from "@oscarpalmer/atoms/is";
|
|
3
|
+
//#region src/validation/value.validation.ts
|
|
3
4
|
function validateObject(obj, properties) {
|
|
4
5
|
if (!isPlainObject(obj)) return false;
|
|
5
6
|
const propertiesLength = properties.length;
|
|
@@ -25,7 +26,7 @@ function validateValue(type, property, value) {
|
|
|
25
26
|
default: return validators[type](value) && (property.validators[type]?.every((validator) => validator(value)) ?? true);
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
+
const validators = {
|
|
29
30
|
array: Array.isArray,
|
|
30
31
|
bigint: (value) => typeof value === "bigint",
|
|
31
32
|
boolean: (value) => typeof value === "boolean",
|
|
@@ -38,4 +39,5 @@ var validators = {
|
|
|
38
39
|
symbol: (value) => typeof value === "symbol",
|
|
39
40
|
undefined: (value) => value === void 0
|
|
40
41
|
};
|
|
42
|
+
//#endregion
|
|
41
43
|
export { validateObject };
|
package/package.json
CHANGED
|
@@ -1,49 +1,54 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
2
|
+
"name": "@oscarpalmer/jhunal",
|
|
3
|
+
"version": "0.17.0",
|
|
4
|
+
"description": "Flies free beneath the glistening moons…",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"schema",
|
|
7
|
+
"validation"
|
|
8
|
+
],
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"author": {
|
|
11
|
+
"name": "Oscar Palmér",
|
|
12
|
+
"url": "https://oscarpalmer.se"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/oscarpalmer/jhunal.git"
|
|
17
|
+
},
|
|
18
|
+
"files": [
|
|
19
|
+
"dist",
|
|
20
|
+
"src"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"module": "./dist/index.mjs",
|
|
24
|
+
"types": "./dist/index.d.mts",
|
|
25
|
+
"exports": {
|
|
26
|
+
"./package.json": "./package.json",
|
|
27
|
+
".": {
|
|
28
|
+
"types": "./dist/index.d.mts",
|
|
29
|
+
"default": "./dist/index.mjs"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"build": "npx vp pack && npm run tsdown:build",
|
|
34
|
+
"tsdown:build": "npx tsdown -c ./tsdown.config.ts",
|
|
35
|
+
"tsdown:watch": "npx tsdown -c ./tsdown.config.ts --watch",
|
|
36
|
+
"test": "npx vp test run --coverage",
|
|
37
|
+
"test:leak": "npx vp test run --detect-async-leaks --coverage",
|
|
38
|
+
"watch": "npx vite build --watch"
|
|
39
|
+
},
|
|
40
|
+
"dependencies": {
|
|
41
|
+
"@oscarpalmer/atoms": "^0.168"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@types/node": "^25.5",
|
|
45
|
+
"@vitest/coverage-istanbul": "^4.1",
|
|
46
|
+
"jsdom": "^29.0",
|
|
47
|
+
"tsdown": "^0.21",
|
|
48
|
+
"typescript": "^5.9",
|
|
49
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@latest",
|
|
50
|
+
"vite-plus": "latest",
|
|
51
|
+
"vitest": "npm:@voidzero-dev/vite-plus-test@latest"
|
|
52
|
+
},
|
|
53
|
+
"packageManager": "npm@11.11.1"
|
|
49
54
|
}
|
package/src/constants.ts
CHANGED
|
@@ -2,8 +2,6 @@ import type {ValueName} from './models';
|
|
|
2
2
|
|
|
3
3
|
export const ERROR_NAME = 'SchematicError';
|
|
4
4
|
|
|
5
|
-
export const EXPRESSION_PROPERTY = /(^|\.)\$(required|type|validators)(\.|$)/;
|
|
6
|
-
|
|
7
5
|
export const MESSAGE_CONSTRUCTOR = 'Expected a constructor function';
|
|
8
6
|
|
|
9
7
|
export const MESSAGE_SCHEMA_INVALID_EMPTY = 'Schema must have at least one property';
|
|
@@ -11,8 +9,10 @@ export const MESSAGE_SCHEMA_INVALID_EMPTY = 'Schema must have at least one prope
|
|
|
11
9
|
export const MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED =
|
|
12
10
|
"'<key>.<property>' property is not allowed for schemas in $type";
|
|
13
11
|
|
|
14
|
-
export const
|
|
15
|
-
"'
|
|
12
|
+
export const MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE =
|
|
13
|
+
"'<>' property must not be 'null' or 'undefined'";
|
|
14
|
+
|
|
15
|
+
export const MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
|
|
16
16
|
|
|
17
17
|
export const MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
18
18
|
|
package/src/models.ts
CHANGED
|
@@ -135,8 +135,8 @@ type InferSchemaEntryValue<Value> =
|
|
|
135
135
|
? Model
|
|
136
136
|
: Value extends SchemaProperty
|
|
137
137
|
? InferPropertyType<Value['$type']>
|
|
138
|
-
: Value extends
|
|
139
|
-
? Infer<
|
|
138
|
+
: Value extends PlainSchema
|
|
139
|
+
? Infer<Value & Schema>
|
|
140
140
|
: Value extends ValueName
|
|
141
141
|
? Values[Value & ValueName]
|
|
142
142
|
: Value extends Schema
|
|
@@ -154,11 +154,7 @@ type IsOptionalProperty<Value> = Value extends SchemaProperty
|
|
|
154
154
|
? Value['$required'] extends false
|
|
155
155
|
? true
|
|
156
156
|
: false
|
|
157
|
-
:
|
|
158
|
-
? Value extends {$required: false}
|
|
159
|
-
? true
|
|
160
|
-
: false
|
|
161
|
-
: false;
|
|
157
|
+
: false;
|
|
162
158
|
|
|
163
159
|
/**
|
|
164
160
|
* Extracts the last member from a union type by leveraging intersection of function return types
|
|
@@ -188,25 +184,6 @@ type MapToSchemaPropertyTypes<Value extends unknown[]> = Value extends [infer He
|
|
|
188
184
|
? [ToSchemaPropertyTypeEach<Head>, ...MapToSchemaPropertyTypes<Tail>]
|
|
189
185
|
: [];
|
|
190
186
|
|
|
191
|
-
/**
|
|
192
|
-
* A nested schema definition that may include a `$required` flag alongside arbitrary string-keyed properties
|
|
193
|
-
*
|
|
194
|
-
* @example
|
|
195
|
-
* ```ts
|
|
196
|
-
* const address: NestedSchema = {
|
|
197
|
-
* $required: false,
|
|
198
|
-
* street: 'string',
|
|
199
|
-
* city: 'string',
|
|
200
|
-
* };
|
|
201
|
-
* ```
|
|
202
|
-
*/
|
|
203
|
-
export type NestedSchema = {
|
|
204
|
-
/**
|
|
205
|
-
* Whether the nested schema is required (defaults to `true`)
|
|
206
|
-
*/
|
|
207
|
-
$required?: boolean;
|
|
208
|
-
} & Schema;
|
|
209
|
-
|
|
210
187
|
/**
|
|
211
188
|
* Extracts keys from an object type that are optional
|
|
212
189
|
*
|
|
@@ -220,7 +197,11 @@ type OptionalKeys<Value> = {
|
|
|
220
197
|
* A generic schema allowing {@link NestedSchema}, {@link SchemaEntry}, or arrays of {@link SchemaEntry} as values
|
|
221
198
|
*/
|
|
222
199
|
type PlainSchema = {
|
|
223
|
-
[key: string]:
|
|
200
|
+
[key: string]: PlainSchema | SchemaEntry | SchemaEntry[] | undefined;
|
|
201
|
+
} & {
|
|
202
|
+
$required?: never;
|
|
203
|
+
$type?: never;
|
|
204
|
+
$validators?: never;
|
|
224
205
|
};
|
|
225
206
|
|
|
226
207
|
/**
|
|
@@ -271,7 +252,7 @@ export type Schema = SchemaIndex;
|
|
|
271
252
|
*/
|
|
272
253
|
type SchemaEntry =
|
|
273
254
|
| Constructor
|
|
274
|
-
|
|
|
255
|
+
| PlainSchema
|
|
275
256
|
| SchemaProperty
|
|
276
257
|
| Schematic<unknown>
|
|
277
258
|
| ValueName
|
|
@@ -281,7 +262,7 @@ type SchemaEntry =
|
|
|
281
262
|
* Index signature interface backing {@link Schema}, allowing string-keyed entries of {@link NestedSchema}, {@link SchemaEntry}, or arrays of {@link SchemaEntry}
|
|
282
263
|
*/
|
|
283
264
|
interface SchemaIndex {
|
|
284
|
-
[key: string]:
|
|
265
|
+
[key: string]: PlainSchema | SchemaEntry | SchemaEntry[];
|
|
285
266
|
}
|
|
286
267
|
|
|
287
268
|
/**
|
|
@@ -327,12 +308,7 @@ type SchemaPropertyType =
|
|
|
327
308
|
| ((value: unknown) => boolean);
|
|
328
309
|
|
|
329
310
|
/**
|
|
330
|
-
* A custom error class for
|
|
331
|
-
*
|
|
332
|
-
* @example
|
|
333
|
-
* ```ts
|
|
334
|
-
* throw new SchematicError('Expected a string, received a number');
|
|
335
|
-
* ```
|
|
311
|
+
* A custom error class for schematic validation failures
|
|
336
312
|
*/
|
|
337
313
|
export class SchematicError extends Error {
|
|
338
314
|
constructor(message: string) {
|
|
@@ -360,11 +336,9 @@ type ToSchemaPropertyType<Value> = UnwrapSingle<
|
|
|
360
336
|
*
|
|
361
337
|
* @template Value - type to convert
|
|
362
338
|
*/
|
|
363
|
-
type ToSchemaPropertyTypeEach<Value> = Value extends
|
|
364
|
-
?
|
|
365
|
-
: Value
|
|
366
|
-
? TypedSchema<Value>
|
|
367
|
-
: ToValueType<Value>;
|
|
339
|
+
type ToSchemaPropertyTypeEach<Value> = Value extends PlainObject
|
|
340
|
+
? TypedSchema<Value>
|
|
341
|
+
: ToValueType<Value>;
|
|
368
342
|
|
|
369
343
|
/**
|
|
370
344
|
* Converts a type into its corresponding {@link ValueName}-representation
|
package/src/schematic.ts
CHANGED
|
@@ -30,7 +30,7 @@ export class Schematic<Model> {
|
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
32
|
* Does the value match the schema?
|
|
33
|
-
* @param value
|
|
33
|
+
* @param value Value to validate
|
|
34
34
|
* @returns `true` if the value matches the schema, otherwise `false`
|
|
35
35
|
*/
|
|
36
36
|
is(value: unknown): value is Model {
|
|
@@ -40,8 +40,8 @@ export class Schematic<Model> {
|
|
|
40
40
|
|
|
41
41
|
/**
|
|
42
42
|
* Create a schematic from a schema
|
|
43
|
-
* @template Model
|
|
44
|
-
* @param schema
|
|
43
|
+
* @template Model Schema type
|
|
44
|
+
* @param schema Schema to create the schematic from
|
|
45
45
|
* @throws Throws {@link SchematicError} if the schema can not be converted into a schematic
|
|
46
46
|
* @returns A schematic for the given schema
|
|
47
47
|
*/
|
|
@@ -49,8 +49,8 @@ export function schematic<Model extends Schema>(schema: Model): Schematic<Infer<
|
|
|
49
49
|
|
|
50
50
|
/**
|
|
51
51
|
* Create a schematic from a typed schema
|
|
52
|
-
* @template Model
|
|
53
|
-
* @param schema
|
|
52
|
+
* @template Model Existing type
|
|
53
|
+
* @param schema Typed schema to create the schematic from
|
|
54
54
|
* @throws Throws {@link SchematicError} if the schema can not be converted into a schematic
|
|
55
55
|
* @returns A schematic for the given typed schema
|
|
56
56
|
*/
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import {isConstructor, isPlainObject} from '@oscarpalmer/atoms/is';
|
|
2
2
|
import type {PlainObject} from '@oscarpalmer/atoms/models';
|
|
3
|
-
import {join} from '@oscarpalmer/atoms/string
|
|
3
|
+
import {join} from '@oscarpalmer/atoms/string';
|
|
4
4
|
import {
|
|
5
|
-
EXPRESSION_PROPERTY,
|
|
6
5
|
MESSAGE_SCHEMA_INVALID_EMPTY,
|
|
7
6
|
MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED,
|
|
7
|
+
MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE,
|
|
8
8
|
MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED,
|
|
9
9
|
MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE,
|
|
10
10
|
MESSAGE_VALIDATOR_INVALID_KEY,
|
|
@@ -74,12 +74,17 @@ export function getProperties(
|
|
|
74
74
|
for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
75
75
|
const key = keys[keyIndex];
|
|
76
76
|
|
|
77
|
-
if (EXPRESSION_PROPERTY.test(key)) {
|
|
78
|
-
continue;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
77
|
const value = original[key];
|
|
82
78
|
|
|
79
|
+
if (value == null) {
|
|
80
|
+
throw new SchematicError(
|
|
81
|
+
MESSAGE_SCHEMA_INVALID_PROPERTY_NULLABLE.replace(
|
|
82
|
+
TEMPLATE_PATTERN,
|
|
83
|
+
join([prefix, key], '.'),
|
|
84
|
+
),
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
|
|
83
88
|
const types: ValidatedPropertyType[] = [];
|
|
84
89
|
|
|
85
90
|
let required = true;
|
package/dist/index.js
DELETED
package/dist/models.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
*/
|
|
10
|
-
var SchematicError = class extends Error {
|
|
11
|
-
constructor(message) {
|
|
12
|
-
super(message);
|
|
13
|
-
this.name = ERROR_NAME;
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
export { SchematicError };
|
package/types/constants.d.ts
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
export declare const ERROR_NAME = "SchematicError";
|
|
2
|
-
export declare const EXPRESSION_PROPERTY: RegExp;
|
|
3
|
-
export declare const MESSAGE_CONSTRUCTOR = "Expected a constructor function";
|
|
4
|
-
export declare const MESSAGE_SCHEMA_INVALID_EMPTY = "Schema must have at least one property";
|
|
5
|
-
export declare const MESSAGE_SCHEMA_INVALID_PROPERTY_DISALLOWED = "'<key>.<property>' property is not allowed for schemas in $type";
|
|
6
|
-
export declare const MESSAGE_SCHEMA_INVALID_PROPERTY_REQUIRED = "'<>.$required' property must be a boolean";
|
|
7
|
-
export declare const MESSAGE_SCHEMA_INVALID_PROPERTY_TYPE = "'<>' property must be of a valid type";
|
|
8
|
-
export declare const MESSAGE_SCHEMA_INVALID_TYPE = "Schema must be an object";
|
|
9
|
-
export declare const MESSAGE_VALIDATOR_INVALID_KEY = "Validator '<>' does not exist";
|
|
10
|
-
export declare const MESSAGE_VALIDATOR_INVALID_TYPE = "Validators must be an object";
|
|
11
|
-
export declare const MESSAGE_VALIDATOR_INVALID_VALUE = "Validator '<>' must be a function or an array of functions";
|
|
12
|
-
export declare const PROPERTY_REQUIRED = "$required";
|
|
13
|
-
export declare const PROPERTY_TYPE = "$type";
|
|
14
|
-
export declare const PROPERTY_VALIDATORS = "$validators";
|
|
15
|
-
export declare const SCHEMATIC_NAME = "$schematic";
|
|
16
|
-
export declare const TEMPLATE_PATTERN = "<>";
|
|
17
|
-
export declare const TEMPLATE_PATTERN_KEY = "<key>";
|
|
18
|
-
export declare const TEMPLATE_PATTERN_PROPERTY = "<property>";
|
|
19
|
-
export declare const TYPE_OBJECT = "object";
|
|
20
|
-
export declare const TYPE_UNDEFINED = "undefined";
|
|
21
|
-
export declare const VALIDATABLE_TYPES: Set<keyof import("./models").Values>;
|
|
22
|
-
export declare const TYPE_ALL: Set<keyof import("./models").Values>;
|
package/types/index.d.ts
DELETED
package/types/schematic.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import type { PlainObject } from '@oscarpalmer/atoms/models';
|
|
2
|
-
import { type Infer, type Schema, type TypedSchema, type ValidatedProperty } from './models';
|
|
3
|
-
/**
|
|
4
|
-
* A schematic for validating objects
|
|
5
|
-
*/
|
|
6
|
-
export declare class Schematic<Model> {
|
|
7
|
-
#private;
|
|
8
|
-
private readonly $schematic;
|
|
9
|
-
constructor(properties: ValidatedProperty[]);
|
|
10
|
-
/**
|
|
11
|
-
* Does the value match the schema?
|
|
12
|
-
* @param value - Value to validate
|
|
13
|
-
* @returns `true` if the value matches the schema, otherwise `false`
|
|
14
|
-
*/
|
|
15
|
-
is(value: unknown): value is Model;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Create a schematic from a schema
|
|
19
|
-
* @template Model - Schema type
|
|
20
|
-
* @param schema - Schema to create the schematic from
|
|
21
|
-
* @throws Throws {@link SchematicError} if the schema can not be converted into a schematic
|
|
22
|
-
* @returns A schematic for the given schema
|
|
23
|
-
*/
|
|
24
|
-
export declare function schematic<Model extends Schema>(schema: Model): Schematic<Infer<Model>>;
|
|
25
|
-
/**
|
|
26
|
-
* Create a schematic from a typed schema
|
|
27
|
-
* @template Model - Existing type
|
|
28
|
-
* @param schema - Typed schema to create the schematic from
|
|
29
|
-
* @throws Throws {@link SchematicError} if the schema can not be converted into a schematic
|
|
30
|
-
* @returns A schematic for the given typed schema
|
|
31
|
-
*/
|
|
32
|
-
export declare function schematic<Model extends PlainObject>(schema: TypedSchema<Model>): Schematic<Model>;
|