@oscarpalmer/jhunal 0.2.0 → 0.4.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/README.md +2 -0
- package/dist/helpers.cjs +45 -0
- package/dist/helpers.js +41 -0
- package/dist/index.cjs +4 -106
- package/dist/index.js +1 -107
- package/dist/is.cjs +19 -0
- package/dist/is.js +14 -0
- package/dist/model.cjs +2 -0
- package/dist/model.js +1 -0
- package/dist/schematic.cjs +17 -0
- package/dist/schematic.js +13 -0
- package/dist/validation/schema.validation.cjs +54 -0
- package/dist/validation/schema.validation.js +49 -0
- package/dist/validation/type.validation.cjs +33 -0
- package/dist/validation/type.validation.js +29 -0
- package/dist/validation/value.validation.cjs +35 -0
- package/dist/validation/value.validation.js +31 -0
- package/package.json +1 -1
- package/src/helpers.ts +49 -0
- package/src/index.ts +2 -282
- package/src/is.ts +22 -0
- package/src/model.ts +168 -0
- package/src/schematic.ts +30 -0
- package/src/validation/schema.validation.ts +63 -0
- package/src/validation/type.validation.ts +34 -0
- package/src/validation/value.validation.ts +46 -0
- package/types/helpers.d.cts +43 -0
- package/types/helpers.d.ts +2 -0
- package/types/index.d.cts +25 -14
- package/types/index.d.ts +2 -75
- package/types/is.d.cts +15 -0
- package/types/is.d.ts +3 -0
- package/types/model.d.cts +365 -0
- package/types/model.d.ts +91 -0
- package/types/schematic.d.cts +360 -0
- package/types/schematic.d.ts +9 -0
- package/types/validation/schema.validation.d.cts +55 -0
- package/types/validation/schema.validation.d.ts +3 -0
- package/types/validation/type.validation.d.cts +43 -0
- package/types/validation/type.validation.d.ts +2 -0
- package/types/validation/value.validation.d.cts +43 -0
- package/types/validation/value.validation.d.ts +2 -0
package/README.md
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# Jhunal
|
|
2
2
|
|
|
3
|
+
[](https://www.npmjs.com/package/@oscarpalmer/jhunal) [](https://github.com/oscarpalmer/jhunal/actions/workflows/test.yml)
|
|
4
|
+
|
|
3
5
|
> Jhunal, sage God of Runes,
|
|
4
6
|
> Flies free beneath the glistening moons.
|
|
5
7
|
> A night owl who's so droll,
|
package/dist/helpers.cjs
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const is = require('./is.cjs');
|
|
6
|
+
const validation_schema_validation = require('./validation/schema.validation.cjs');
|
|
7
|
+
|
|
8
|
+
function getTypes(value) {
|
|
9
|
+
const returned = [];
|
|
10
|
+
const values = Array.isArray(value) ? value : [value];
|
|
11
|
+
const { length } = values;
|
|
12
|
+
for (let index = 0; index < length; index += 1) {
|
|
13
|
+
const type = values[index];
|
|
14
|
+
switch (true) {
|
|
15
|
+
case is.isSchematic(type):
|
|
16
|
+
returned.push(type);
|
|
17
|
+
break;
|
|
18
|
+
case (typeof type === "string" && types.has(type)):
|
|
19
|
+
returned.push(type);
|
|
20
|
+
break;
|
|
21
|
+
case (typeof type === "object" && type !== null): {
|
|
22
|
+
returned.push(validation_schema_validation.validateSchema(type));
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return returned;
|
|
28
|
+
}
|
|
29
|
+
const types = /* @__PURE__ */ new Set([
|
|
30
|
+
"array",
|
|
31
|
+
"bigint",
|
|
32
|
+
"boolean",
|
|
33
|
+
"date",
|
|
34
|
+
"date-like",
|
|
35
|
+
"function",
|
|
36
|
+
"null",
|
|
37
|
+
"number",
|
|
38
|
+
"numerical",
|
|
39
|
+
"object",
|
|
40
|
+
"string",
|
|
41
|
+
"symbol",
|
|
42
|
+
"undefined"
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
exports.getTypes = getTypes;
|
package/dist/helpers.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { isSchematic } from './is.js';
|
|
2
|
+
import { validateSchema } from './validation/schema.validation.js';
|
|
3
|
+
|
|
4
|
+
function getTypes(value) {
|
|
5
|
+
const returned = [];
|
|
6
|
+
const values = Array.isArray(value) ? value : [value];
|
|
7
|
+
const { length } = values;
|
|
8
|
+
for (let index = 0; index < length; index += 1) {
|
|
9
|
+
const type = values[index];
|
|
10
|
+
switch (true) {
|
|
11
|
+
case isSchematic(type):
|
|
12
|
+
returned.push(type);
|
|
13
|
+
break;
|
|
14
|
+
case (typeof type === "string" && types.has(type)):
|
|
15
|
+
returned.push(type);
|
|
16
|
+
break;
|
|
17
|
+
case (typeof type === "object" && type !== null): {
|
|
18
|
+
returned.push(validateSchema(type));
|
|
19
|
+
break;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return returned;
|
|
24
|
+
}
|
|
25
|
+
const types = /* @__PURE__ */ new Set([
|
|
26
|
+
"array",
|
|
27
|
+
"bigint",
|
|
28
|
+
"boolean",
|
|
29
|
+
"date",
|
|
30
|
+
"date-like",
|
|
31
|
+
"function",
|
|
32
|
+
"null",
|
|
33
|
+
"number",
|
|
34
|
+
"numerical",
|
|
35
|
+
"object",
|
|
36
|
+
"string",
|
|
37
|
+
"symbol",
|
|
38
|
+
"undefined"
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
export { getTypes };
|
package/dist/index.cjs
CHANGED
|
@@ -2,110 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
|
-
const
|
|
6
|
-
"array",
|
|
7
|
-
"bigint",
|
|
8
|
-
"boolean",
|
|
9
|
-
"date",
|
|
10
|
-
"function",
|
|
11
|
-
"null",
|
|
12
|
-
"number",
|
|
13
|
-
"object",
|
|
14
|
-
"string",
|
|
15
|
-
"symbol",
|
|
16
|
-
"undefined"
|
|
17
|
-
]);
|
|
18
|
-
const validators = {
|
|
19
|
-
array: Array.isArray,
|
|
20
|
-
bigint: (value) => typeof value === "bigint",
|
|
21
|
-
boolean: (value) => typeof value === "boolean",
|
|
22
|
-
date: (value) => value instanceof Date,
|
|
23
|
-
function: (value) => typeof value === "function",
|
|
24
|
-
null: (value) => value === null,
|
|
25
|
-
number: (value) => typeof value === "number",
|
|
26
|
-
object: (value) => typeof value === "object" && value !== null,
|
|
27
|
-
string: (value) => typeof value === "string",
|
|
28
|
-
symbol: (value) => typeof value === "symbol",
|
|
29
|
-
undefined: (value) => value === void 0
|
|
30
|
-
};
|
|
31
|
-
function getTypes(value) {
|
|
32
|
-
return (Array.isArray(value) ? value : [value]).filter(
|
|
33
|
-
(item) => types.has(item)
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
function getValidatedSchema(schema) {
|
|
37
|
-
const validated = {
|
|
38
|
-
keys: [],
|
|
39
|
-
length: 0,
|
|
40
|
-
properties: {}
|
|
41
|
-
};
|
|
42
|
-
if (typeof schema !== "object" || schema === null) {
|
|
43
|
-
return validated;
|
|
44
|
-
}
|
|
45
|
-
const keys = Object.keys(schema);
|
|
46
|
-
const { length } = keys;
|
|
47
|
-
for (let index = 0; index < length; index += 1) {
|
|
48
|
-
const key = keys[index];
|
|
49
|
-
const value = schema[key];
|
|
50
|
-
let required = true;
|
|
51
|
-
let valueTypes;
|
|
52
|
-
if (Array.isArray(value)) {
|
|
53
|
-
valueTypes = getTypes(value);
|
|
54
|
-
} else if (typeof value === "object" && value !== null) {
|
|
55
|
-
if (typeof value.required === "boolean") {
|
|
56
|
-
required = value.required;
|
|
57
|
-
}
|
|
58
|
-
valueTypes = getTypes(value.type);
|
|
59
|
-
} else {
|
|
60
|
-
valueTypes = getTypes(value);
|
|
61
|
-
}
|
|
62
|
-
if (valueTypes.length > 0) {
|
|
63
|
-
if (!required && !valueTypes.includes("undefined")) {
|
|
64
|
-
valueTypes.push("undefined");
|
|
65
|
-
}
|
|
66
|
-
validated.keys.push(key);
|
|
67
|
-
validated.properties[key] = {
|
|
68
|
-
required,
|
|
69
|
-
types: valueTypes
|
|
70
|
-
};
|
|
71
|
-
validated.length += 1;
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
return validated;
|
|
75
|
-
}
|
|
76
|
-
function schematic(schema) {
|
|
77
|
-
const validated = getValidatedSchema(schema);
|
|
78
|
-
const canValidate = validated.length > 0;
|
|
79
|
-
return Object.freeze({
|
|
80
|
-
is: (value) => canValidate && validate(validated, value)
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
function validate(validated, obj) {
|
|
84
|
-
if (typeof obj !== "object" || obj === null) {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
outer: for (let index = 0; index < validated.length; index += 1) {
|
|
88
|
-
const key = validated.keys[index];
|
|
89
|
-
const property = validated.properties[key];
|
|
90
|
-
const value = obj[key];
|
|
91
|
-
if (value === void 0 && property.required && !property.types.includes("undefined")) {
|
|
92
|
-
return false;
|
|
93
|
-
}
|
|
94
|
-
const typesLength = property.types.length;
|
|
95
|
-
if (typesLength === 1) {
|
|
96
|
-
if (!validators[property.types[0]](value)) {
|
|
97
|
-
return false;
|
|
98
|
-
}
|
|
99
|
-
continue;
|
|
100
|
-
}
|
|
101
|
-
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
102
|
-
if (validators[property.types[typeIndex]](value)) {
|
|
103
|
-
continue outer;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
return false;
|
|
107
|
-
}
|
|
108
|
-
return true;
|
|
109
|
-
}
|
|
5
|
+
const schematic = require('./schematic.cjs');
|
|
110
6
|
|
|
111
|
-
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
exports.schematic = schematic.schematic;
|
package/dist/index.js
CHANGED
|
@@ -1,107 +1 @@
|
|
|
1
|
-
|
|
2
|
-
"array",
|
|
3
|
-
"bigint",
|
|
4
|
-
"boolean",
|
|
5
|
-
"date",
|
|
6
|
-
"function",
|
|
7
|
-
"null",
|
|
8
|
-
"number",
|
|
9
|
-
"object",
|
|
10
|
-
"string",
|
|
11
|
-
"symbol",
|
|
12
|
-
"undefined"
|
|
13
|
-
]);
|
|
14
|
-
const validators = {
|
|
15
|
-
array: Array.isArray,
|
|
16
|
-
bigint: (value) => typeof value === "bigint",
|
|
17
|
-
boolean: (value) => typeof value === "boolean",
|
|
18
|
-
date: (value) => value instanceof Date,
|
|
19
|
-
function: (value) => typeof value === "function",
|
|
20
|
-
null: (value) => value === null,
|
|
21
|
-
number: (value) => typeof value === "number",
|
|
22
|
-
object: (value) => typeof value === "object" && value !== null,
|
|
23
|
-
string: (value) => typeof value === "string",
|
|
24
|
-
symbol: (value) => typeof value === "symbol",
|
|
25
|
-
undefined: (value) => value === void 0
|
|
26
|
-
};
|
|
27
|
-
function getTypes(value) {
|
|
28
|
-
return (Array.isArray(value) ? value : [value]).filter(
|
|
29
|
-
(item) => types.has(item)
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
function getValidatedSchema(schema) {
|
|
33
|
-
const validated = {
|
|
34
|
-
keys: [],
|
|
35
|
-
length: 0,
|
|
36
|
-
properties: {}
|
|
37
|
-
};
|
|
38
|
-
if (typeof schema !== "object" || schema === null) {
|
|
39
|
-
return validated;
|
|
40
|
-
}
|
|
41
|
-
const keys = Object.keys(schema);
|
|
42
|
-
const { length } = keys;
|
|
43
|
-
for (let index = 0; index < length; index += 1) {
|
|
44
|
-
const key = keys[index];
|
|
45
|
-
const value = schema[key];
|
|
46
|
-
let required = true;
|
|
47
|
-
let valueTypes;
|
|
48
|
-
if (Array.isArray(value)) {
|
|
49
|
-
valueTypes = getTypes(value);
|
|
50
|
-
} else if (typeof value === "object" && value !== null) {
|
|
51
|
-
if (typeof value.required === "boolean") {
|
|
52
|
-
required = value.required;
|
|
53
|
-
}
|
|
54
|
-
valueTypes = getTypes(value.type);
|
|
55
|
-
} else {
|
|
56
|
-
valueTypes = getTypes(value);
|
|
57
|
-
}
|
|
58
|
-
if (valueTypes.length > 0) {
|
|
59
|
-
if (!required && !valueTypes.includes("undefined")) {
|
|
60
|
-
valueTypes.push("undefined");
|
|
61
|
-
}
|
|
62
|
-
validated.keys.push(key);
|
|
63
|
-
validated.properties[key] = {
|
|
64
|
-
required,
|
|
65
|
-
types: valueTypes
|
|
66
|
-
};
|
|
67
|
-
validated.length += 1;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
return validated;
|
|
71
|
-
}
|
|
72
|
-
function schematic(schema) {
|
|
73
|
-
const validated = getValidatedSchema(schema);
|
|
74
|
-
const canValidate = validated.length > 0;
|
|
75
|
-
return Object.freeze({
|
|
76
|
-
is: (value) => canValidate && validate(validated, value)
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
function validate(validated, obj) {
|
|
80
|
-
if (typeof obj !== "object" || obj === null) {
|
|
81
|
-
return false;
|
|
82
|
-
}
|
|
83
|
-
outer: for (let index = 0; index < validated.length; index += 1) {
|
|
84
|
-
const key = validated.keys[index];
|
|
85
|
-
const property = validated.properties[key];
|
|
86
|
-
const value = obj[key];
|
|
87
|
-
if (value === void 0 && property.required && !property.types.includes("undefined")) {
|
|
88
|
-
return false;
|
|
89
|
-
}
|
|
90
|
-
const typesLength = property.types.length;
|
|
91
|
-
if (typesLength === 1) {
|
|
92
|
-
if (!validators[property.types[0]](value)) {
|
|
93
|
-
return false;
|
|
94
|
-
}
|
|
95
|
-
continue;
|
|
96
|
-
}
|
|
97
|
-
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
98
|
-
if (validators[property.types[typeIndex]](value)) {
|
|
99
|
-
continue outer;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return false;
|
|
103
|
-
}
|
|
104
|
-
return true;
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export { schematic };
|
|
1
|
+
export { schematic } from './schematic.js';
|
package/dist/is.cjs
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
function isDateLike(value) {
|
|
6
|
+
if (value instanceof Date) {
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
if (typeof value === "number") {
|
|
10
|
+
return value >= -864e13 && value <= 864e13;
|
|
11
|
+
}
|
|
12
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
13
|
+
}
|
|
14
|
+
function isSchematic(value) {
|
|
15
|
+
return typeof value === "object" && value !== null && "$schematic" in value && value.$schematic === true;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.isDateLike = isDateLike;
|
|
19
|
+
exports.isSchematic = isSchematic;
|
package/dist/is.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
function isDateLike(value) {
|
|
2
|
+
if (value instanceof Date) {
|
|
3
|
+
return true;
|
|
4
|
+
}
|
|
5
|
+
if (typeof value === "number") {
|
|
6
|
+
return value >= -864e13 && value <= 864e13;
|
|
7
|
+
}
|
|
8
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
9
|
+
}
|
|
10
|
+
function isSchematic(value) {
|
|
11
|
+
return typeof value === "object" && value !== null && "$schematic" in value && value.$schematic === true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { isDateLike, isSchematic };
|
package/dist/model.cjs
ADDED
package/dist/model.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const validation_schema_validation = require('./validation/schema.validation.cjs');
|
|
6
|
+
const validation_value_validation = require('./validation/value.validation.cjs');
|
|
7
|
+
|
|
8
|
+
function schematic(schema) {
|
|
9
|
+
const validated = validation_schema_validation.validateSchema(schema);
|
|
10
|
+
const canValidate = validated.length > 0;
|
|
11
|
+
return Object.freeze({
|
|
12
|
+
$schematic: true,
|
|
13
|
+
is: (value) => canValidate && validation_value_validation.validateValue(validated, value)
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
exports.schematic = schematic;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { validateSchema } from './validation/schema.validation.js';
|
|
2
|
+
import { validateValue } from './validation/value.validation.js';
|
|
3
|
+
|
|
4
|
+
function schematic(schema) {
|
|
5
|
+
const validated = validateSchema(schema);
|
|
6
|
+
const canValidate = validated.length > 0;
|
|
7
|
+
return Object.freeze({
|
|
8
|
+
$schematic: true,
|
|
9
|
+
is: (value) => canValidate && validateValue(validated, value)
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { schematic };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const helpers = require('../helpers.cjs');
|
|
6
|
+
|
|
7
|
+
function validateSchema(schema) {
|
|
8
|
+
if (validatedSchemas.has(schema)) {
|
|
9
|
+
return validatedSchemas.get(schema);
|
|
10
|
+
}
|
|
11
|
+
const validated = {
|
|
12
|
+
keys: [],
|
|
13
|
+
length: 0,
|
|
14
|
+
properties: {}
|
|
15
|
+
};
|
|
16
|
+
if (typeof schema !== "object" || schema === null) {
|
|
17
|
+
return validated;
|
|
18
|
+
}
|
|
19
|
+
const keys = Object.keys(schema);
|
|
20
|
+
const { length } = keys;
|
|
21
|
+
for (let index = 0; index < length; index += 1) {
|
|
22
|
+
const key = keys[index];
|
|
23
|
+
const value = schema[key];
|
|
24
|
+
let required = true;
|
|
25
|
+
let types;
|
|
26
|
+
if (Array.isArray(value)) {
|
|
27
|
+
types = helpers.getTypes(value);
|
|
28
|
+
} else if (typeof value === "object" && value !== null) {
|
|
29
|
+
if (typeof value.required === "boolean") {
|
|
30
|
+
required = value.required;
|
|
31
|
+
}
|
|
32
|
+
types = helpers.getTypes(value.type);
|
|
33
|
+
} else {
|
|
34
|
+
types = helpers.getTypes(value);
|
|
35
|
+
}
|
|
36
|
+
if (types.length > 0) {
|
|
37
|
+
if (!required && !types.includes("undefined")) {
|
|
38
|
+
types.push("undefined");
|
|
39
|
+
}
|
|
40
|
+
validated.keys.push(key);
|
|
41
|
+
validated.properties[key] = {
|
|
42
|
+
required,
|
|
43
|
+
types
|
|
44
|
+
};
|
|
45
|
+
validated.length += 1;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
validatedSchemas.set(schema, validated);
|
|
49
|
+
return validated;
|
|
50
|
+
}
|
|
51
|
+
const validatedSchemas = /* @__PURE__ */ new WeakMap();
|
|
52
|
+
|
|
53
|
+
exports.validateSchema = validateSchema;
|
|
54
|
+
exports.validatedSchemas = validatedSchemas;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { getTypes } from '../helpers.js';
|
|
2
|
+
|
|
3
|
+
function validateSchema(schema) {
|
|
4
|
+
if (validatedSchemas.has(schema)) {
|
|
5
|
+
return validatedSchemas.get(schema);
|
|
6
|
+
}
|
|
7
|
+
const validated = {
|
|
8
|
+
keys: [],
|
|
9
|
+
length: 0,
|
|
10
|
+
properties: {}
|
|
11
|
+
};
|
|
12
|
+
if (typeof schema !== "object" || schema === null) {
|
|
13
|
+
return validated;
|
|
14
|
+
}
|
|
15
|
+
const keys = Object.keys(schema);
|
|
16
|
+
const { length } = keys;
|
|
17
|
+
for (let index = 0; index < length; index += 1) {
|
|
18
|
+
const key = keys[index];
|
|
19
|
+
const value = schema[key];
|
|
20
|
+
let required = true;
|
|
21
|
+
let types;
|
|
22
|
+
if (Array.isArray(value)) {
|
|
23
|
+
types = getTypes(value);
|
|
24
|
+
} else if (typeof value === "object" && value !== null) {
|
|
25
|
+
if (typeof value.required === "boolean") {
|
|
26
|
+
required = value.required;
|
|
27
|
+
}
|
|
28
|
+
types = getTypes(value.type);
|
|
29
|
+
} else {
|
|
30
|
+
types = getTypes(value);
|
|
31
|
+
}
|
|
32
|
+
if (types.length > 0) {
|
|
33
|
+
if (!required && !types.includes("undefined")) {
|
|
34
|
+
types.push("undefined");
|
|
35
|
+
}
|
|
36
|
+
validated.keys.push(key);
|
|
37
|
+
validated.properties[key] = {
|
|
38
|
+
required,
|
|
39
|
+
types
|
|
40
|
+
};
|
|
41
|
+
validated.length += 1;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
validatedSchemas.set(schema, validated);
|
|
45
|
+
return validated;
|
|
46
|
+
}
|
|
47
|
+
const validatedSchemas = /* @__PURE__ */ new WeakMap();
|
|
48
|
+
|
|
49
|
+
export { validateSchema, validatedSchemas };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const is = require('../is.cjs');
|
|
6
|
+
const validation_value_validation = require('./value.validation.cjs');
|
|
7
|
+
|
|
8
|
+
function validateType(type, value) {
|
|
9
|
+
if (typeof type === "string") {
|
|
10
|
+
return validators[type](value);
|
|
11
|
+
}
|
|
12
|
+
if (is.isSchematic(type)) {
|
|
13
|
+
return type.is(value);
|
|
14
|
+
}
|
|
15
|
+
return validation_value_validation.validateValue(type, value);
|
|
16
|
+
}
|
|
17
|
+
const validators = {
|
|
18
|
+
array: Array.isArray,
|
|
19
|
+
bigint: (value) => typeof value === "bigint",
|
|
20
|
+
boolean: (value) => typeof value === "boolean",
|
|
21
|
+
date: (value) => value instanceof Date,
|
|
22
|
+
"date-like": is.isDateLike,
|
|
23
|
+
function: (value) => typeof value === "function",
|
|
24
|
+
null: (value) => value === null,
|
|
25
|
+
number: (value) => typeof value === "number",
|
|
26
|
+
numerical: (value) => validators.bigint(value) || validators.number(value),
|
|
27
|
+
object: (value) => typeof value === "object" && value !== null,
|
|
28
|
+
string: (value) => typeof value === "string",
|
|
29
|
+
symbol: (value) => typeof value === "symbol",
|
|
30
|
+
undefined: (value) => value === void 0
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
exports.validateType = validateType;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { isDateLike, isSchematic } from '../is.js';
|
|
2
|
+
import { validateValue } from './value.validation.js';
|
|
3
|
+
|
|
4
|
+
function validateType(type, value) {
|
|
5
|
+
if (typeof type === "string") {
|
|
6
|
+
return validators[type](value);
|
|
7
|
+
}
|
|
8
|
+
if (isSchematic(type)) {
|
|
9
|
+
return type.is(value);
|
|
10
|
+
}
|
|
11
|
+
return validateValue(type, value);
|
|
12
|
+
}
|
|
13
|
+
const validators = {
|
|
14
|
+
array: Array.isArray,
|
|
15
|
+
bigint: (value) => typeof value === "bigint",
|
|
16
|
+
boolean: (value) => typeof value === "boolean",
|
|
17
|
+
date: (value) => value instanceof Date,
|
|
18
|
+
"date-like": isDateLike,
|
|
19
|
+
function: (value) => typeof value === "function",
|
|
20
|
+
null: (value) => value === null,
|
|
21
|
+
number: (value) => typeof value === "number",
|
|
22
|
+
numerical: (value) => validators.bigint(value) || validators.number(value),
|
|
23
|
+
object: (value) => typeof value === "object" && value !== null,
|
|
24
|
+
string: (value) => typeof value === "string",
|
|
25
|
+
symbol: (value) => typeof value === "symbol",
|
|
26
|
+
undefined: (value) => value === void 0
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export { validateType };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
|
|
5
|
+
const validation_type_validation = require('./type.validation.cjs');
|
|
6
|
+
|
|
7
|
+
function validateValue(validated, obj) {
|
|
8
|
+
if (typeof obj !== "object" || obj === null) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
|
+
outer: for (let keyIndex = 0; keyIndex < validated.length; keyIndex += 1) {
|
|
12
|
+
const key = validated.keys[keyIndex];
|
|
13
|
+
const property = validated.properties[key];
|
|
14
|
+
const value = obj[key];
|
|
15
|
+
if (value === void 0 && property.required && !property.types.includes("undefined")) {
|
|
16
|
+
return false;
|
|
17
|
+
}
|
|
18
|
+
const typesLength = property.types.length;
|
|
19
|
+
if (typesLength === 1) {
|
|
20
|
+
if (!validation_type_validation.validateType(property.types[0], value)) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
26
|
+
if (validation_type_validation.validateType(property.types[typeIndex], value)) {
|
|
27
|
+
continue outer;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.validateValue = validateValue;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { validateType } from './type.validation.js';
|
|
2
|
+
|
|
3
|
+
function validateValue(validated, obj) {
|
|
4
|
+
if (typeof obj !== "object" || obj === null) {
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
outer: for (let keyIndex = 0; keyIndex < validated.length; keyIndex += 1) {
|
|
8
|
+
const key = validated.keys[keyIndex];
|
|
9
|
+
const property = validated.properties[key];
|
|
10
|
+
const value = obj[key];
|
|
11
|
+
if (value === void 0 && property.required && !property.types.includes("undefined")) {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
const typesLength = property.types.length;
|
|
15
|
+
if (typesLength === 1) {
|
|
16
|
+
if (!validateType(property.types[0], value)) {
|
|
17
|
+
return false;
|
|
18
|
+
}
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) {
|
|
22
|
+
if (validateType(property.types[typeIndex], value)) {
|
|
23
|
+
continue outer;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return false;
|
|
27
|
+
}
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export { validateValue };
|
package/package.json
CHANGED
package/src/helpers.ts
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import {isSchematic} from './is';
|
|
2
|
+
import type {ValidatedPropertyType, Values} from './model';
|
|
3
|
+
import {validateSchema} from './validation/schema.validation';
|
|
4
|
+
|
|
5
|
+
export function getTypes(value: unknown): ValidatedPropertyType[] {
|
|
6
|
+
const returned: ValidatedPropertyType[] = [];
|
|
7
|
+
const values = Array.isArray(value) ? value : [value];
|
|
8
|
+
const {length} = values;
|
|
9
|
+
|
|
10
|
+
for (let index = 0; index < length; index += 1) {
|
|
11
|
+
const type = values[index];
|
|
12
|
+
|
|
13
|
+
switch (true) {
|
|
14
|
+
case isSchematic(type):
|
|
15
|
+
returned.push(type);
|
|
16
|
+
break;
|
|
17
|
+
|
|
18
|
+
case typeof type === 'string' && types.has(type as never):
|
|
19
|
+
returned.push(type as never);
|
|
20
|
+
break;
|
|
21
|
+
|
|
22
|
+
case typeof type === 'object' && type !== null: {
|
|
23
|
+
returned.push(validateSchema(type));
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
default:
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return returned;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const types = new Set<keyof Values>([
|
|
36
|
+
'array',
|
|
37
|
+
'bigint',
|
|
38
|
+
'boolean',
|
|
39
|
+
'date',
|
|
40
|
+
'date-like',
|
|
41
|
+
'function',
|
|
42
|
+
'null',
|
|
43
|
+
'number',
|
|
44
|
+
'numerical',
|
|
45
|
+
'object',
|
|
46
|
+
'string',
|
|
47
|
+
'symbol',
|
|
48
|
+
'undefined',
|
|
49
|
+
]);
|