@lucania/schema 1.0.0 → 1.0.2
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/build/index.d.ts +2 -1
- package/build/index.js +43 -0
- package/package.json +2 -2
package/build/index.d.ts
CHANGED
|
@@ -64,7 +64,7 @@ export declare namespace Schema {
|
|
|
64
64
|
function registerPrimitive<TypeName extends keyof Map>(typeName: TypeName): void;
|
|
65
65
|
function isSchemaPrimitive(value: any): value is Schema.Primitive;
|
|
66
66
|
function isSchemaOrCompound(value: any): value is Schema.OrCompound;
|
|
67
|
-
function isSchemaAndCompound(value: any): value is Schema.
|
|
67
|
+
function isSchemaAndCompound(value: any): value is Schema.AndCompound;
|
|
68
68
|
function isSchemaMeta(value: any): value is Schema.Meta;
|
|
69
69
|
function isSchemaDynamic(value: any): value is Schema.Dynamic;
|
|
70
70
|
function isSchemaArray(value: any): value is Schema.Array;
|
|
@@ -73,6 +73,7 @@ export declare namespace Schema {
|
|
|
73
73
|
function getType(value: any): string;
|
|
74
74
|
function build<Schema extends Schema.Any>(schema: Schema): Schema;
|
|
75
75
|
function validate<Schema extends Any>(schema: Schema, source: Source<Schema>): Model<Schema>;
|
|
76
|
+
function clone<Schema extends Schema.Any>(schema: Schema): Schema;
|
|
76
77
|
function assert(condition: boolean, message?: string): asserts condition;
|
|
77
78
|
type ValidationErrorType = "missing" | "incorrectType" | "invalidSchema" | "failedCustomValidator";
|
|
78
79
|
class Error extends globalThis.Error {
|
package/build/index.js
CHANGED
|
@@ -218,6 +218,49 @@
|
|
|
218
218
|
return new ValidationError("invalidSchema", schema, source, path, originalSchema, originalSource);
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
|
+
function clone(schema) {
|
|
222
|
+
if (isSchemaPrimitive(schema)) {
|
|
223
|
+
return schema;
|
|
224
|
+
}
|
|
225
|
+
else if (isSchemaOrCompound(schema)) {
|
|
226
|
+
const [a, _, b] = schema;
|
|
227
|
+
return [clone(a), "or", clone(b)];
|
|
228
|
+
}
|
|
229
|
+
else if (isSchemaAndCompound(schema)) {
|
|
230
|
+
const [a, _, b] = schema;
|
|
231
|
+
return [clone(a), "and", clone(b)];
|
|
232
|
+
}
|
|
233
|
+
else if (isSchemaMeta(schema)) {
|
|
234
|
+
const meta = {
|
|
235
|
+
type: clone(schema.type),
|
|
236
|
+
required: schema.required,
|
|
237
|
+
};
|
|
238
|
+
if ("default" in schema) {
|
|
239
|
+
meta.default = schema.default;
|
|
240
|
+
}
|
|
241
|
+
if ("validate" in schema) {
|
|
242
|
+
meta.validate = schema.validate;
|
|
243
|
+
}
|
|
244
|
+
return meta;
|
|
245
|
+
}
|
|
246
|
+
else if (isSchemaDynamic(schema)) {
|
|
247
|
+
return { $: clone(schema.$) };
|
|
248
|
+
}
|
|
249
|
+
else if (isSchemaArray(schema)) {
|
|
250
|
+
return [schema.map(clone)];
|
|
251
|
+
}
|
|
252
|
+
else if (isSchemaHierarchy(schema)) {
|
|
253
|
+
const hierarchy = {};
|
|
254
|
+
for (const key in schema) {
|
|
255
|
+
hierarchy[key] = clone(schema[key]);
|
|
256
|
+
}
|
|
257
|
+
return hierarchy;
|
|
258
|
+
}
|
|
259
|
+
else {
|
|
260
|
+
throw new Error("Invalid Schema.");
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
Schema.clone = clone;
|
|
221
264
|
function assert(condition, message) {
|
|
222
265
|
if (!condition) {
|
|
223
266
|
throw new Schema.Error(message);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucania/schema",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A schema module for compile-time and runtime type checking.",
|
|
5
5
|
"main": "./build/index.js",
|
|
6
6
|
"types": "./build/index.d.ts",
|
|
@@ -37,4 +37,4 @@
|
|
|
37
37
|
"tslib": "^2.6.0",
|
|
38
38
|
"typescript": "^5.1.6"
|
|
39
39
|
}
|
|
40
|
-
}
|
|
40
|
+
}
|