@oscarpalmer/jhunal 0.4.0 → 0.6.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/index.js +2 -1
- package/dist/is.js +4 -9
- package/dist/jhunal.full.js +211 -0
- package/dist/model.js +0 -1
- package/dist/schematic.js +19 -11
- package/dist/validation/schema.validation.js +81 -47
- package/dist/validation/value.validation.js +39 -29
- package/package.json +17 -22
- package/src/index.ts +2 -2
- package/src/is.ts +2 -1
- package/src/model.ts +157 -123
- package/src/schematic.ts +37 -18
- package/src/validation/schema.validation.ts +131 -37
- package/src/validation/value.validation.ts +36 -15
- package/types/index.d.ts +2 -2
- package/types/is.d.ts +1 -1
- package/types/model.d.ts +81 -55
- package/types/schematic.d.ts +17 -3
- package/types/validation/schema.validation.d.ts +1 -2
- package/types/validation/value.validation.d.ts +2 -1
- package/dist/helpers.cjs +0 -45
- package/dist/helpers.js +0 -41
- package/dist/index.cjs +0 -9
- package/dist/is.cjs +0 -19
- package/dist/model.cjs +0 -2
- package/dist/schematic.cjs +0 -17
- package/dist/validation/schema.validation.cjs +0 -54
- package/dist/validation/type.validation.cjs +0 -33
- package/dist/validation/type.validation.js +0 -29
- package/dist/validation/value.validation.cjs +0 -35
- package/src/helpers.ts +0 -49
- package/src/validation/type.validation.ts +0 -34
- package/types/helpers.d.cts +0 -43
- package/types/helpers.d.ts +0 -2
- package/types/index.d.cts +0 -360
- package/types/is.d.cts +0 -15
- package/types/model.d.cts +0 -365
- package/types/schematic.d.cts +0 -360
- package/types/validation/schema.validation.d.cts +0 -55
- package/types/validation/type.validation.d.cts +0 -43
- package/types/validation/type.validation.d.ts +0 -2
- package/types/validation/value.validation.d.cts +0 -43
package/dist/index.js
CHANGED
|
@@ -1 +1,2 @@
|
|
|
1
|
-
|
|
1
|
+
import { schematic } from "./schematic.js";
|
|
2
|
+
export { schematic };
|
package/dist/is.js
CHANGED
|
@@ -1,14 +1,9 @@
|
|
|
1
1
|
function isDateLike(value) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
if (typeof value === "number") {
|
|
6
|
-
return value >= -864e13 && value <= 864e13;
|
|
7
|
-
}
|
|
8
|
-
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
2
|
+
if (value instanceof Date) return true;
|
|
3
|
+
if (typeof value === "number") return value >= -864e13 && value <= 864e13;
|
|
4
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
9
5
|
}
|
|
10
6
|
function isSchematic(value) {
|
|
11
|
-
|
|
7
|
+
return typeof value === "object" && value !== null && "$schematic" in value && value.$schematic === true;
|
|
12
8
|
}
|
|
13
|
-
|
|
14
9
|
export { isDateLike, isSchematic };
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
function compact(array, strict) {
|
|
2
|
+
if (!Array.isArray(array)) return [];
|
|
3
|
+
const { length } = array;
|
|
4
|
+
const isStrict = strict ?? false;
|
|
5
|
+
const compacted = [];
|
|
6
|
+
for (let index = 0; index < length; index += 1) {
|
|
7
|
+
const item = array[index];
|
|
8
|
+
if (isStrict && !!item || !isStrict && item != null) compacted.push(item);
|
|
9
|
+
}
|
|
10
|
+
return compacted;
|
|
11
|
+
}
|
|
12
|
+
function getString(value) {
|
|
13
|
+
if (typeof value === "string") return value;
|
|
14
|
+
if (value == null) return "";
|
|
15
|
+
if (typeof value === "function") return getString(value());
|
|
16
|
+
if (typeof value !== "object") return String(value);
|
|
17
|
+
const asString = String(value.valueOf?.() ?? value);
|
|
18
|
+
return asString.startsWith("[object ") ? JSON.stringify(value) : asString;
|
|
19
|
+
}
|
|
20
|
+
function join(value, delimiter) {
|
|
21
|
+
return compact(value).map(getString).join(typeof delimiter === "string" ? delimiter : "");
|
|
22
|
+
}
|
|
23
|
+
function isArrayOrPlainObject(value) {
|
|
24
|
+
return Array.isArray(value) || isPlainObject(value);
|
|
25
|
+
}
|
|
26
|
+
function isPlainObject(value) {
|
|
27
|
+
if (value === null || typeof value !== "object") return false;
|
|
28
|
+
if (Symbol.toStringTag in value || Symbol.iterator in value) return false;
|
|
29
|
+
const prototype = Object.getPrototypeOf(value);
|
|
30
|
+
return prototype === null || prototype === Object.prototype || Object.getPrototypeOf(prototype) === null;
|
|
31
|
+
}
|
|
32
|
+
new Set([
|
|
33
|
+
Int8Array,
|
|
34
|
+
Uint8Array,
|
|
35
|
+
Uint8ClampedArray,
|
|
36
|
+
Int16Array,
|
|
37
|
+
Uint16Array,
|
|
38
|
+
Int32Array,
|
|
39
|
+
Uint32Array,
|
|
40
|
+
Float32Array,
|
|
41
|
+
Float64Array,
|
|
42
|
+
BigInt64Array,
|
|
43
|
+
BigUint64Array
|
|
44
|
+
]);
|
|
45
|
+
function flattenObject(value, depth, smushed, prefix) {
|
|
46
|
+
if (depth >= MAX_DEPTH) return {};
|
|
47
|
+
if (smushed.has(value)) return smushed.get(value);
|
|
48
|
+
const keys = Object.keys(value);
|
|
49
|
+
const { length } = keys;
|
|
50
|
+
const flattened = {};
|
|
51
|
+
for (let index = 0; index < length; index += 1) {
|
|
52
|
+
const key = keys[index];
|
|
53
|
+
const val = value[key];
|
|
54
|
+
if (isArrayOrPlainObject(val)) Object.assign(flattened, {
|
|
55
|
+
[join([prefix, key], ".")]: Array.isArray(val) ? [...val] : { ...val },
|
|
56
|
+
...flattenObject(val, depth + 1, smushed, join([prefix, key], "."))
|
|
57
|
+
});
|
|
58
|
+
else flattened[join([prefix, key], ".")] = val;
|
|
59
|
+
}
|
|
60
|
+
smushed.set(value, flattened);
|
|
61
|
+
return flattened;
|
|
62
|
+
}
|
|
63
|
+
function smush(value) {
|
|
64
|
+
return typeof value === "object" && value !== null ? flattenObject(value, 0, /* @__PURE__ */ new WeakMap()) : {};
|
|
65
|
+
}
|
|
66
|
+
var MAX_DEPTH = 100;
|
|
67
|
+
function isDateLike(value) {
|
|
68
|
+
if (value instanceof Date) return true;
|
|
69
|
+
if (typeof value === "number") return value >= -864e13 && value <= 864e13;
|
|
70
|
+
return typeof value === "string" && !Number.isNaN(Date.parse(value));
|
|
71
|
+
}
|
|
72
|
+
function isSchematic(value) {
|
|
73
|
+
return typeof value === "object" && value !== null && "$schematic" in value && value.$schematic === true;
|
|
74
|
+
}
|
|
75
|
+
function addPropertyType(to, key, values, required) {
|
|
76
|
+
if (to.keys.set.has(key)) to.properties[key].types.push(...values);
|
|
77
|
+
else {
|
|
78
|
+
to.keys.array.push(key);
|
|
79
|
+
to.keys.set.add(key);
|
|
80
|
+
to.properties[key] = {
|
|
81
|
+
required,
|
|
82
|
+
types: values
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
if (!required && !to.properties[key].types.includes("undefined")) to.properties[key].types.push("undefined");
|
|
86
|
+
}
|
|
87
|
+
function getTypes(value, validated, prefix) {
|
|
88
|
+
const propertyTypes = [];
|
|
89
|
+
const values = Array.isArray(value) ? value : [value];
|
|
90
|
+
const { length } = values;
|
|
91
|
+
for (let index = 0; index < length; index += 1) {
|
|
92
|
+
const type = values[index];
|
|
93
|
+
if (isSchematic(type) || typeof type === "string" && types.has(type)) {
|
|
94
|
+
propertyTypes.push(type);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
if (typeof type !== "object" || type === null) continue;
|
|
98
|
+
if ("$type" in type) {
|
|
99
|
+
propertyTypes.push(...getTypes(type.$type, validated, prefix));
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
addPropertyType(validated, prefix, ["object"], typeof type.$required === "boolean" ? type.$required : true);
|
|
103
|
+
propertyTypes.push("object");
|
|
104
|
+
getValidatedSchema(type, validated, prefix);
|
|
105
|
+
}
|
|
106
|
+
return propertyTypes;
|
|
107
|
+
}
|
|
108
|
+
function getValidatedSchema(schema, validated, prefix) {
|
|
109
|
+
const smushed = smush(schema);
|
|
110
|
+
const keys = Object.keys(smushed);
|
|
111
|
+
const { length } = keys;
|
|
112
|
+
const arrayKeys = /* @__PURE__ */ new Set();
|
|
113
|
+
const noPrefix = prefix == null;
|
|
114
|
+
prefix = noPrefix ? "" : `${prefix}.`;
|
|
115
|
+
for (let index = 0; index < length; index += 1) {
|
|
116
|
+
const key = keys[index];
|
|
117
|
+
const value = smushed[key];
|
|
118
|
+
if (Array.isArray(value)) arrayKeys.add(key);
|
|
119
|
+
if (/\.\$(required|type)(\.|$)/.test(key)) continue;
|
|
120
|
+
if (/\d+/.test(key) && arrayKeys.has(key.replace(/\.\d+$/, ""))) continue;
|
|
121
|
+
let required = true;
|
|
122
|
+
if (typeof value === "object" && value !== null && "$required" in value) required = typeof value.$required === "boolean" ? value.$required : true;
|
|
123
|
+
const prefixedKey = `${prefix}${key}`;
|
|
124
|
+
const types$1 = getTypes(value, validated, prefixedKey);
|
|
125
|
+
if (types$1.length > 0) addPropertyType(validated, prefixedKey, types$1, required);
|
|
126
|
+
}
|
|
127
|
+
if (noPrefix) validated.keys.array.sort();
|
|
128
|
+
return validated;
|
|
129
|
+
}
|
|
130
|
+
function validateSchema(schema) {
|
|
131
|
+
const validated = {
|
|
132
|
+
keys: {
|
|
133
|
+
array: [],
|
|
134
|
+
set: /* @__PURE__ */ new Set()
|
|
135
|
+
},
|
|
136
|
+
properties: {}
|
|
137
|
+
};
|
|
138
|
+
return typeof schema === "object" && schema !== null ? getValidatedSchema(schema, validated) : validated;
|
|
139
|
+
}
|
|
140
|
+
const types = new Set([
|
|
141
|
+
"array",
|
|
142
|
+
"bigint",
|
|
143
|
+
"boolean",
|
|
144
|
+
"date",
|
|
145
|
+
"date-like",
|
|
146
|
+
"function",
|
|
147
|
+
"null",
|
|
148
|
+
"number",
|
|
149
|
+
"numerical",
|
|
150
|
+
"object",
|
|
151
|
+
"string",
|
|
152
|
+
"symbol",
|
|
153
|
+
"undefined"
|
|
154
|
+
]);
|
|
155
|
+
function validateType(type, value) {
|
|
156
|
+
return typeof type === "string" ? validators[type](value) : type.is(value);
|
|
157
|
+
}
|
|
158
|
+
function validateValue(validated, obj) {
|
|
159
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
160
|
+
const { keys, properties } = validated;
|
|
161
|
+
const keysLength = keys.array.length;
|
|
162
|
+
const smushed = smush(obj);
|
|
163
|
+
outer: for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
164
|
+
const key = keys.array[keyIndex];
|
|
165
|
+
const property = properties[key];
|
|
166
|
+
const value = smushed[key];
|
|
167
|
+
if (value === void 0 && property.required && !property.types.includes("undefined")) return false;
|
|
168
|
+
const typesLength = property.types.length;
|
|
169
|
+
if (typesLength === 1) {
|
|
170
|
+
if (!validateType(property.types[0], value)) return false;
|
|
171
|
+
continue;
|
|
172
|
+
}
|
|
173
|
+
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) if (validateType(property.types[typeIndex], value)) continue outer;
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
const validators = {
|
|
179
|
+
array: Array.isArray,
|
|
180
|
+
bigint: (value) => typeof value === "bigint",
|
|
181
|
+
boolean: (value) => typeof value === "boolean",
|
|
182
|
+
date: (value) => value instanceof Date,
|
|
183
|
+
"date-like": isDateLike,
|
|
184
|
+
function: (value) => typeof value === "function",
|
|
185
|
+
null: (value) => value === null,
|
|
186
|
+
number: (value) => typeof value === "number" && !Number.isNaN(value),
|
|
187
|
+
numerical: (value) => validators.bigint(value) || validators.number(value),
|
|
188
|
+
object: (value) => typeof value === "object" && value !== null,
|
|
189
|
+
string: (value) => typeof value === "string",
|
|
190
|
+
symbol: (value) => typeof value === "symbol",
|
|
191
|
+
undefined: (value) => value === void 0
|
|
192
|
+
};
|
|
193
|
+
var Schematic = class {
|
|
194
|
+
#schema;
|
|
195
|
+
#validatable;
|
|
196
|
+
get validatable() {
|
|
197
|
+
return this.#validatable;
|
|
198
|
+
}
|
|
199
|
+
constructor(schema) {
|
|
200
|
+
Object.defineProperty(this, "$schematic", { value: true });
|
|
201
|
+
this.#schema = validateSchema(schema);
|
|
202
|
+
this.#validatable = this.#schema.keys.array.length > 0;
|
|
203
|
+
}
|
|
204
|
+
is(value) {
|
|
205
|
+
return this.#validatable && validateValue(this.#schema, value);
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
function schematic(schema) {
|
|
209
|
+
return new Schematic(schema);
|
|
210
|
+
}
|
|
211
|
+
export { schematic };
|
package/dist/model.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
|
package/dist/schematic.js
CHANGED
|
@@ -1,13 +1,21 @@
|
|
|
1
|
-
import { validateSchema } from
|
|
2
|
-
import { validateValue } from
|
|
3
|
-
|
|
1
|
+
import { validateSchema } from "./validation/schema.validation.js";
|
|
2
|
+
import { validateValue } from "./validation/value.validation.js";
|
|
3
|
+
var Schematic = class {
|
|
4
|
+
#schema;
|
|
5
|
+
#validatable;
|
|
6
|
+
get validatable() {
|
|
7
|
+
return this.#validatable;
|
|
8
|
+
}
|
|
9
|
+
constructor(schema) {
|
|
10
|
+
Object.defineProperty(this, "$schematic", { value: true });
|
|
11
|
+
this.#schema = validateSchema(schema);
|
|
12
|
+
this.#validatable = this.#schema.keys.array.length > 0;
|
|
13
|
+
}
|
|
14
|
+
is(value) {
|
|
15
|
+
return this.#validatable && validateValue(this.#schema, value);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
4
18
|
function schematic(schema) {
|
|
5
|
-
|
|
6
|
-
const canValidate = validated.length > 0;
|
|
7
|
-
return Object.freeze({
|
|
8
|
-
$schematic: true,
|
|
9
|
-
is: (value) => canValidate && validateValue(validated, value)
|
|
10
|
-
});
|
|
19
|
+
return new Schematic(schema);
|
|
11
20
|
}
|
|
12
|
-
|
|
13
|
-
export { schematic };
|
|
21
|
+
export { Schematic, schematic };
|
|
@@ -1,49 +1,83 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { isSchematic } from "../is.js";
|
|
2
|
+
import { smush } from "@oscarpalmer/atoms/value";
|
|
3
|
+
function addPropertyType(to, key, values, required) {
|
|
4
|
+
if (to.keys.set.has(key)) to.properties[key].types.push(...values);
|
|
5
|
+
else {
|
|
6
|
+
to.keys.array.push(key);
|
|
7
|
+
to.keys.set.add(key);
|
|
8
|
+
to.properties[key] = {
|
|
9
|
+
required,
|
|
10
|
+
types: values
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
if (!required && !to.properties[key].types.includes("undefined")) to.properties[key].types.push("undefined");
|
|
14
|
+
}
|
|
15
|
+
function getTypes(value, validated, prefix) {
|
|
16
|
+
const propertyTypes = [];
|
|
17
|
+
const values = Array.isArray(value) ? value : [value];
|
|
18
|
+
const { length } = values;
|
|
19
|
+
for (let index = 0; index < length; index += 1) {
|
|
20
|
+
const type = values[index];
|
|
21
|
+
if (isSchematic(type) || typeof type === "string" && types.has(type)) {
|
|
22
|
+
propertyTypes.push(type);
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
if (typeof type !== "object" || type === null) continue;
|
|
26
|
+
if ("$type" in type) {
|
|
27
|
+
propertyTypes.push(...getTypes(type.$type, validated, prefix));
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
addPropertyType(validated, prefix, ["object"], typeof type.$required === "boolean" ? type.$required : true);
|
|
31
|
+
propertyTypes.push("object");
|
|
32
|
+
getValidatedSchema(type, validated, prefix);
|
|
33
|
+
}
|
|
34
|
+
return propertyTypes;
|
|
35
|
+
}
|
|
36
|
+
function getValidatedSchema(schema, validated, prefix) {
|
|
37
|
+
const smushed = smush(schema);
|
|
38
|
+
const keys = Object.keys(smushed);
|
|
39
|
+
const { length } = keys;
|
|
40
|
+
const arrayKeys = /* @__PURE__ */ new Set();
|
|
41
|
+
const noPrefix = prefix == null;
|
|
42
|
+
prefix = noPrefix ? "" : `${prefix}.`;
|
|
43
|
+
for (let index = 0; index < length; index += 1) {
|
|
44
|
+
const key = keys[index];
|
|
45
|
+
const value = smushed[key];
|
|
46
|
+
if (Array.isArray(value)) arrayKeys.add(key);
|
|
47
|
+
if (/\.\$(required|type)(\.|$)/.test(key)) continue;
|
|
48
|
+
if (/\d+/.test(key) && arrayKeys.has(key.replace(/\.\d+$/, ""))) continue;
|
|
49
|
+
let required = true;
|
|
50
|
+
if (typeof value === "object" && value !== null && "$required" in value) required = typeof value.$required === "boolean" ? value.$required : true;
|
|
51
|
+
const prefixedKey = `${prefix}${key}`;
|
|
52
|
+
const types$1 = getTypes(value, validated, prefixedKey);
|
|
53
|
+
if (types$1.length > 0) addPropertyType(validated, prefixedKey, types$1, required);
|
|
54
|
+
}
|
|
55
|
+
if (noPrefix) validated.keys.array.sort();
|
|
56
|
+
return validated;
|
|
57
|
+
}
|
|
3
58
|
function validateSchema(schema) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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;
|
|
59
|
+
const validated = {
|
|
60
|
+
keys: {
|
|
61
|
+
array: [],
|
|
62
|
+
set: /* @__PURE__ */ new Set()
|
|
63
|
+
},
|
|
64
|
+
properties: {}
|
|
65
|
+
};
|
|
66
|
+
return typeof schema === "object" && schema !== null ? getValidatedSchema(schema, validated) : validated;
|
|
46
67
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
68
|
+
var types = new Set([
|
|
69
|
+
"array",
|
|
70
|
+
"bigint",
|
|
71
|
+
"boolean",
|
|
72
|
+
"date",
|
|
73
|
+
"date-like",
|
|
74
|
+
"function",
|
|
75
|
+
"null",
|
|
76
|
+
"number",
|
|
77
|
+
"numerical",
|
|
78
|
+
"object",
|
|
79
|
+
"string",
|
|
80
|
+
"symbol",
|
|
81
|
+
"undefined"
|
|
82
|
+
]);
|
|
83
|
+
export { validateSchema };
|
|
@@ -1,31 +1,41 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import { isDateLike } from "../is.js";
|
|
2
|
+
import { smush } from "@oscarpalmer/atoms/value";
|
|
3
|
+
function validateType(type, value) {
|
|
4
|
+
return typeof type === "string" ? validators[type](value) : type.is(value);
|
|
5
|
+
}
|
|
3
6
|
function validateValue(validated, obj) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (validateType(property.types[typeIndex], value)) {
|
|
23
|
-
continue outer;
|
|
24
|
-
}
|
|
25
|
-
}
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
7
|
+
if (typeof obj !== "object" || obj === null) return false;
|
|
8
|
+
const { keys, properties } = validated;
|
|
9
|
+
const keysLength = keys.array.length;
|
|
10
|
+
const smushed = smush(obj);
|
|
11
|
+
outer: for (let keyIndex = 0; keyIndex < keysLength; keyIndex += 1) {
|
|
12
|
+
const key = keys.array[keyIndex];
|
|
13
|
+
const property = properties[key];
|
|
14
|
+
const value = smushed[key];
|
|
15
|
+
if (value === void 0 && property.required && !property.types.includes("undefined")) return false;
|
|
16
|
+
const typesLength = property.types.length;
|
|
17
|
+
if (typesLength === 1) {
|
|
18
|
+
if (!validateType(property.types[0], value)) return false;
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
for (let typeIndex = 0; typeIndex < typesLength; typeIndex += 1) if (validateType(property.types[typeIndex], value)) continue outer;
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
return true;
|
|
29
25
|
}
|
|
30
|
-
|
|
31
|
-
|
|
26
|
+
var validators = {
|
|
27
|
+
array: Array.isArray,
|
|
28
|
+
bigint: (value) => typeof value === "bigint",
|
|
29
|
+
boolean: (value) => typeof value === "boolean",
|
|
30
|
+
date: (value) => value instanceof Date,
|
|
31
|
+
"date-like": isDateLike,
|
|
32
|
+
function: (value) => typeof value === "function",
|
|
33
|
+
null: (value) => value === null,
|
|
34
|
+
number: (value) => typeof value === "number" && !Number.isNaN(value),
|
|
35
|
+
numerical: (value) => validators.bigint(value) || validators.number(value),
|
|
36
|
+
object: (value) => typeof value === "object" && value !== null,
|
|
37
|
+
string: (value) => typeof value === "string",
|
|
38
|
+
symbol: (value) => typeof value === "symbol",
|
|
39
|
+
undefined: (value) => value === void 0
|
|
40
|
+
};
|
|
41
|
+
export { validateType, validateValue };
|
package/package.json
CHANGED
|
@@ -4,37 +4,31 @@
|
|
|
4
4
|
"url": "https://oscarpalmer.se"
|
|
5
5
|
},
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"@oscarpalmer/atoms": "^0.
|
|
8
|
-
"type-fest": "^4.39"
|
|
7
|
+
"@oscarpalmer/atoms": "^0.118"
|
|
9
8
|
},
|
|
10
9
|
"description": "Flies free beneath the glistening moons…",
|
|
11
10
|
"devDependencies": {
|
|
12
|
-
"@
|
|
13
|
-
"@
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
"
|
|
19
|
-
"
|
|
11
|
+
"@types/node": "^25",
|
|
12
|
+
"@vitest/coverage-istanbul": "^4",
|
|
13
|
+
"jsdom": "^27.4",
|
|
14
|
+
"oxfmt": "^0.20",
|
|
15
|
+
"oxlint": "^1.35",
|
|
16
|
+
"rolldown": "1.0.0-beta.57",
|
|
17
|
+
"tslib": "^2.8",
|
|
18
|
+
"typescript": "^5.9",
|
|
19
|
+
"vite": "8.0.0-beta.5",
|
|
20
|
+
"vitest": "^4"
|
|
20
21
|
},
|
|
21
22
|
"exports": {
|
|
22
23
|
"./package.json": "./package.json",
|
|
23
24
|
".": {
|
|
24
|
-
"import": {
|
|
25
25
|
"types": "./types/index.d.ts",
|
|
26
26
|
"default": "./dist/index.js"
|
|
27
|
-
},
|
|
28
|
-
"require": {
|
|
29
|
-
"types": "./types/index.d.cts",
|
|
30
|
-
"default": "./dist/index.cjs"
|
|
31
|
-
}
|
|
32
27
|
}
|
|
33
28
|
},
|
|
34
29
|
"files": ["dist", "src", "types"],
|
|
35
|
-
"keywords": [],
|
|
30
|
+
"keywords": ["schema", "validation"],
|
|
36
31
|
"license": "MIT",
|
|
37
|
-
"main": "./dist/index.cjs",
|
|
38
32
|
"module": "./dist/index.js",
|
|
39
33
|
"name": "@oscarpalmer/jhunal",
|
|
40
34
|
"repository": {
|
|
@@ -42,13 +36,14 @@
|
|
|
42
36
|
"url": "git+https://github.com/oscarpalmer/jhunal.git"
|
|
43
37
|
},
|
|
44
38
|
"scripts": {
|
|
45
|
-
"build": "npm run clean && npx vite build && npm run
|
|
39
|
+
"build": "npm run clean && npx vite build && npm run rolldown:build && npx tsc",
|
|
46
40
|
"clean": "rm -rf ./dist && rm -rf ./types && rm -f ./tsconfig.tsbuildinfo",
|
|
41
|
+
"rolldown:build": "npx rolldown -c",
|
|
42
|
+
"rolldown:watch": "npx rolldown -c ./rolldown.config.js --watch",
|
|
47
43
|
"test": "npx vitest --coverage",
|
|
48
|
-
"types": "npx tsc && npx dts-bundle-generator --config ./dts.config.cts --silent",
|
|
49
44
|
"watch": "npx vite build --watch"
|
|
50
45
|
},
|
|
51
46
|
"type": "module",
|
|
52
|
-
"types": "./types/index.d.
|
|
53
|
-
"version": "0.
|
|
47
|
+
"types": "./types/index.d.ts",
|
|
48
|
+
"version": "0.6.0"
|
|
54
49
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type {Schema,
|
|
2
|
-
export
|
|
1
|
+
export type {Schema, TypedSchema} from './model';
|
|
2
|
+
export {schematic, type Schematic} from './schematic';
|
package/src/is.ts
CHANGED