@weser/schema 0.0.18 → 1.0.1
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 +1 -1
- package/dist/src/builders.d.ts +33 -0
- package/dist/src/builders.js +18 -0
- package/dist/src/fromZod.d.ts +3 -0
- package/dist/src/fromZod.js +4 -0
- package/dist/src/guards.d.ts +7 -0
- package/dist/src/guards.js +18 -0
- package/dist/src/index.d.ts +7 -0
- package/dist/src/index.js +7 -0
- package/dist/src/toZod.d.ts +21 -0
- package/dist/src/toZod.js +114 -0
- package/dist/src/types.d.ts +41 -0
- package/dist/src/types.js +1 -0
- package/dist/src/validators.d.ts +8 -0
- package/dist/src/validators.js +77 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/vitest.config.d.ts +2 -0
- package/dist/vitest.config.js +10 -0
- package/package.json +4 -4
package/README.md
CHANGED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { T_JSONSchemaEnum, T_JSONSchemaString, T_JSONSchemaNumber, T_JSONSchemaBoolean, T_JSONSchemaArray, T_JSONSchemaObject, T_JSONSchema } from './types.js';
|
|
2
|
+
export declare function createEnumSchema(values: ReadonlyArray<string>, options?: {
|
|
3
|
+
description?: string;
|
|
4
|
+
default?: string;
|
|
5
|
+
}): T_JSONSchemaEnum;
|
|
6
|
+
export declare function createStringSchema(options?: {
|
|
7
|
+
minLength?: number;
|
|
8
|
+
maxLength?: number;
|
|
9
|
+
format?: 'date' | 'uri' | 'email';
|
|
10
|
+
description?: string;
|
|
11
|
+
default?: string;
|
|
12
|
+
}): T_JSONSchemaString;
|
|
13
|
+
export declare function createNumberSchema(options?: {
|
|
14
|
+
minimum?: number;
|
|
15
|
+
maximum?: number;
|
|
16
|
+
description?: string;
|
|
17
|
+
default?: number;
|
|
18
|
+
}): T_JSONSchemaNumber;
|
|
19
|
+
export declare function createBooleanSchema(options?: {
|
|
20
|
+
description?: string;
|
|
21
|
+
default?: boolean;
|
|
22
|
+
}): T_JSONSchemaBoolean;
|
|
23
|
+
export declare function createArraySchema<T>(items: T_JSONSchema, options?: {
|
|
24
|
+
description?: string;
|
|
25
|
+
default?: Array<T>;
|
|
26
|
+
}): T_JSONSchemaArray<T>;
|
|
27
|
+
export declare function createObjectSchema<T extends Record<string, any>>(properties: {
|
|
28
|
+
[K in keyof T]: T_JSONSchema;
|
|
29
|
+
}, options?: {
|
|
30
|
+
required?: Array<keyof T>;
|
|
31
|
+
description?: string;
|
|
32
|
+
default?: T;
|
|
33
|
+
}): T_JSONSchemaObject<T>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function createEnumSchema(values, options = {}) {
|
|
2
|
+
return { enum: [...values], ...options };
|
|
3
|
+
}
|
|
4
|
+
export function createStringSchema(options = {}) {
|
|
5
|
+
return { type: 'string', ...options };
|
|
6
|
+
}
|
|
7
|
+
export function createNumberSchema(options = {}) {
|
|
8
|
+
return { type: 'number', ...options };
|
|
9
|
+
}
|
|
10
|
+
export function createBooleanSchema(options = {}) {
|
|
11
|
+
return { type: 'boolean', ...options };
|
|
12
|
+
}
|
|
13
|
+
export function createArraySchema(items, options = {}) {
|
|
14
|
+
return { type: 'array', items, ...options };
|
|
15
|
+
}
|
|
16
|
+
export function createObjectSchema(properties, options = {}) {
|
|
17
|
+
return { type: 'object', properties, ...options };
|
|
18
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { T_JSONSchema, T_JSONSchemaBoolean, T_JSONSchemaEnum, T_JSONSchemaNumber, T_JSONSchemaObject, T_JSONSchemaArray, T_JSONSchemaString } from './types.js';
|
|
2
|
+
export declare function isEnumSchema(schema: T_JSONSchema): schema is T_JSONSchemaEnum;
|
|
3
|
+
export declare function isStringSchema(schema: T_JSONSchema): schema is T_JSONSchemaString;
|
|
4
|
+
export declare function isNumberSchema(schema: T_JSONSchema): schema is T_JSONSchemaNumber;
|
|
5
|
+
export declare function isBooleanSchema(schema: T_JSONSchema): schema is T_JSONSchemaBoolean;
|
|
6
|
+
export declare function isObjectSchema(schema: T_JSONSchema): schema is T_JSONSchemaObject;
|
|
7
|
+
export declare function isArraySchema(schema: T_JSONSchema): schema is T_JSONSchemaArray;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function isEnumSchema(schema) {
|
|
2
|
+
return schema.enum !== undefined;
|
|
3
|
+
}
|
|
4
|
+
export function isStringSchema(schema) {
|
|
5
|
+
return schema.type === 'string';
|
|
6
|
+
}
|
|
7
|
+
export function isNumberSchema(schema) {
|
|
8
|
+
return schema.type === 'number';
|
|
9
|
+
}
|
|
10
|
+
export function isBooleanSchema(schema) {
|
|
11
|
+
return schema.type === 'boolean';
|
|
12
|
+
}
|
|
13
|
+
export function isObjectSchema(schema) {
|
|
14
|
+
return schema.type === 'object';
|
|
15
|
+
}
|
|
16
|
+
export function isArraySchema(schema) {
|
|
17
|
+
return schema.type === 'array';
|
|
18
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { T_JSONSchema, T_JSONSchemaBoolean, T_JSONSchemaEnum, T_JSONSchemaNumber, T_JSONSchemaObject, T_JSONSchemaArray, T_JSONSchemaString } from './types.js';
|
|
3
|
+
export default function toZod(schema: T_JSONSchema): z.ZodEnum<{
|
|
4
|
+
[x: string]: string;
|
|
5
|
+
}> | z.ZodDefault<z.ZodEnum<{
|
|
6
|
+
[x: string]: string;
|
|
7
|
+
}>> | z.ZodString | z.ZodDefault<z.ZodString> | z.ZodNumber | z.ZodDefault<z.ZodNumber> | z.ZodBoolean | z.ZodDefault<z.ZodBoolean> | z.ZodObject<{
|
|
8
|
+
[x: string]: z.ZodType<unknown, unknown>;
|
|
9
|
+
}, {}, {}> | z.ZodArray<z.ZodAny> | z.ZodDefault<z.ZodArray<z.ZodAny>>;
|
|
10
|
+
export declare function toZodString({ minLength, maxLength, description, default: _default, format, ...meta }: T_JSONSchemaString): z.ZodString | z.ZodDefault<z.ZodString>;
|
|
11
|
+
export declare function toZodEnum({ enum: _enum, description, default: _default, ...meta }: T_JSONSchemaEnum): z.ZodEnum<{
|
|
12
|
+
[x: string]: string;
|
|
13
|
+
}> | z.ZodDefault<z.ZodEnum<{
|
|
14
|
+
[x: string]: string;
|
|
15
|
+
}>>;
|
|
16
|
+
export declare function toZodNumber({ minimum, maximum, description, default: _default, ...meta }: T_JSONSchemaNumber): z.ZodNumber | z.ZodDefault<z.ZodNumber>;
|
|
17
|
+
export declare function toZodBoolean({ description, default: _default, ...meta }: T_JSONSchemaBoolean): z.ZodBoolean | z.ZodDefault<z.ZodBoolean>;
|
|
18
|
+
export declare function toZodObject({ properties, required, description, ...meta }: T_JSONSchemaObject): z.ZodObject<{
|
|
19
|
+
[x: string]: z.ZodType<unknown, unknown>;
|
|
20
|
+
}, {}, {}>;
|
|
21
|
+
export declare function toZodArray({ items, description, default: _default, ...meta }: T_JSONSchemaArray): z.ZodArray<z.ZodAny> | z.ZodDefault<z.ZodArray<z.ZodAny>>;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { isBooleanSchema, isEnumSchema, isNumberSchema, isObjectSchema, isArraySchema, isStringSchema, } from './guards.js';
|
|
3
|
+
export default function toZod(schema) {
|
|
4
|
+
if (isEnumSchema(schema)) {
|
|
5
|
+
return toZodEnum(schema);
|
|
6
|
+
}
|
|
7
|
+
if (isStringSchema(schema)) {
|
|
8
|
+
return toZodString(schema);
|
|
9
|
+
}
|
|
10
|
+
if (isNumberSchema(schema)) {
|
|
11
|
+
return toZodNumber(schema);
|
|
12
|
+
}
|
|
13
|
+
if (isBooleanSchema(schema)) {
|
|
14
|
+
return toZodBoolean(schema);
|
|
15
|
+
}
|
|
16
|
+
if (isObjectSchema(schema)) {
|
|
17
|
+
return toZodObject(schema);
|
|
18
|
+
}
|
|
19
|
+
if (isArraySchema(schema)) {
|
|
20
|
+
return toZodArray(schema);
|
|
21
|
+
}
|
|
22
|
+
throw new Error('Error converting JSON schema to Zod type.');
|
|
23
|
+
}
|
|
24
|
+
export function toZodString({ minLength, maxLength, description, default: _default, format, ...meta }) {
|
|
25
|
+
let type = z.string();
|
|
26
|
+
if (format === 'uri') {
|
|
27
|
+
type = type.url();
|
|
28
|
+
}
|
|
29
|
+
else if (format === 'date') {
|
|
30
|
+
type = type.date();
|
|
31
|
+
}
|
|
32
|
+
else if (format === 'email') {
|
|
33
|
+
type = type.email();
|
|
34
|
+
}
|
|
35
|
+
type = type.meta({ ...meta, ...(format ? { format } : {}) });
|
|
36
|
+
if (description) {
|
|
37
|
+
type = type.describe(description);
|
|
38
|
+
}
|
|
39
|
+
if (minLength) {
|
|
40
|
+
type = type.min(minLength);
|
|
41
|
+
}
|
|
42
|
+
if (maxLength) {
|
|
43
|
+
type = type.max(maxLength);
|
|
44
|
+
}
|
|
45
|
+
if (_default) {
|
|
46
|
+
return type.default(_default);
|
|
47
|
+
}
|
|
48
|
+
return type;
|
|
49
|
+
}
|
|
50
|
+
export function toZodEnum({ enum: _enum, description, default: _default, ...meta }) {
|
|
51
|
+
let type = z.enum(_enum);
|
|
52
|
+
type = type.meta(meta);
|
|
53
|
+
if (description) {
|
|
54
|
+
type = type.describe(description);
|
|
55
|
+
}
|
|
56
|
+
if (_default) {
|
|
57
|
+
return type.default(_default);
|
|
58
|
+
}
|
|
59
|
+
return type;
|
|
60
|
+
}
|
|
61
|
+
export function toZodNumber({ minimum, maximum, description, default: _default, ...meta }) {
|
|
62
|
+
let type = z.number();
|
|
63
|
+
type = type.meta(meta);
|
|
64
|
+
if (description) {
|
|
65
|
+
type = type.describe(description);
|
|
66
|
+
}
|
|
67
|
+
if (minimum) {
|
|
68
|
+
type = type.min(minimum);
|
|
69
|
+
}
|
|
70
|
+
if (maximum) {
|
|
71
|
+
type = type.max(maximum);
|
|
72
|
+
}
|
|
73
|
+
if (_default) {
|
|
74
|
+
return type.default(_default);
|
|
75
|
+
}
|
|
76
|
+
return type;
|
|
77
|
+
}
|
|
78
|
+
export function toZodBoolean({ description, default: _default, ...meta }) {
|
|
79
|
+
let type = z.boolean();
|
|
80
|
+
type = type.meta(meta);
|
|
81
|
+
if (description) {
|
|
82
|
+
type = type.describe(description);
|
|
83
|
+
}
|
|
84
|
+
if (_default) {
|
|
85
|
+
return type.default(_default);
|
|
86
|
+
}
|
|
87
|
+
return type;
|
|
88
|
+
}
|
|
89
|
+
export function toZodObject({ properties, required, description, ...meta }) {
|
|
90
|
+
const props = Object.entries(properties).reduce((props, [property, schema]) => {
|
|
91
|
+
const type = toZod(schema);
|
|
92
|
+
if (type) {
|
|
93
|
+
props[property] = type;
|
|
94
|
+
}
|
|
95
|
+
return props;
|
|
96
|
+
}, {});
|
|
97
|
+
let type = z.object(props);
|
|
98
|
+
type = type.meta(meta);
|
|
99
|
+
if (description) {
|
|
100
|
+
type = type.describe(description);
|
|
101
|
+
}
|
|
102
|
+
return type;
|
|
103
|
+
}
|
|
104
|
+
export function toZodArray({ items, description, default: _default, ...meta }) {
|
|
105
|
+
let type = z.array(toZod(items));
|
|
106
|
+
type = type.meta(meta);
|
|
107
|
+
if (description) {
|
|
108
|
+
type = type.describe(description);
|
|
109
|
+
}
|
|
110
|
+
if (_default) {
|
|
111
|
+
return type.default(_default);
|
|
112
|
+
}
|
|
113
|
+
return type;
|
|
114
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export type T_JSONValue = string | number | boolean | null | JSONObject | Array<T_JSONValue>;
|
|
2
|
+
export type JSONObject = {
|
|
3
|
+
[key: string]: T_JSONValue;
|
|
4
|
+
};
|
|
5
|
+
export type T_JSONSchemaRaw = {
|
|
6
|
+
description?: string;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
export type T_JSONSchemaEnum = T_JSONSchemaRaw & {
|
|
10
|
+
enum: Array<string>;
|
|
11
|
+
default?: string;
|
|
12
|
+
};
|
|
13
|
+
export type T_JSONSchemaString = T_JSONSchemaRaw & {
|
|
14
|
+
type: 'string';
|
|
15
|
+
minLength?: number;
|
|
16
|
+
maxLength?: number;
|
|
17
|
+
format?: 'date' | 'uri' | 'email';
|
|
18
|
+
default?: string;
|
|
19
|
+
};
|
|
20
|
+
export type T_JSONSchemaNumber = T_JSONSchemaRaw & {
|
|
21
|
+
type: 'number';
|
|
22
|
+
minimum?: number;
|
|
23
|
+
maximum?: number;
|
|
24
|
+
default?: number;
|
|
25
|
+
};
|
|
26
|
+
export type T_JSONSchemaBoolean = T_JSONSchemaRaw & {
|
|
27
|
+
type: 'boolean';
|
|
28
|
+
default?: boolean;
|
|
29
|
+
};
|
|
30
|
+
export interface T_JSONSchemaArray<T = T_JSONValue> extends T_JSONSchemaRaw {
|
|
31
|
+
type: 'array';
|
|
32
|
+
items: T_JSONSchema;
|
|
33
|
+
default?: Array<T>;
|
|
34
|
+
}
|
|
35
|
+
export interface T_JSONSchemaObject<T extends Record<string, any> = Record<string, T_JSONValue>> extends T_JSONSchemaRaw {
|
|
36
|
+
type: 'object';
|
|
37
|
+
required?: Array<keyof T>;
|
|
38
|
+
properties: Record<string, T_JSONSchema>;
|
|
39
|
+
default?: T;
|
|
40
|
+
}
|
|
41
|
+
export type T_JSONSchema = T_JSONSchemaEnum | T_JSONSchemaString | T_JSONSchemaNumber | T_JSONSchemaBoolean | T_JSONSchemaObject | T_JSONSchemaArray;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { T_JSONSchemaEnum, T_JSONSchemaString, T_JSONSchemaNumber, T_JSONSchemaBoolean, T_JSONSchemaObject, T_JSONSchemaArray, T_JSONSchema } from './types.js';
|
|
2
|
+
export declare function isValidEnumSchema(schema: T_JSONSchema): schema is T_JSONSchemaEnum;
|
|
3
|
+
export declare function isValidStringSchema(schema: T_JSONSchema): schema is T_JSONSchemaString;
|
|
4
|
+
export declare function isValidNumberSchema(schema: T_JSONSchema): schema is T_JSONSchemaNumber;
|
|
5
|
+
export declare function isValidBooleanSchema(schema: T_JSONSchema): schema is T_JSONSchemaBoolean;
|
|
6
|
+
export declare function isValidArraySchema(schema: T_JSONSchema): schema is T_JSONSchemaArray;
|
|
7
|
+
export declare function isValidObjectSchema(schema: T_JSONSchema): schema is T_JSONSchemaObject;
|
|
8
|
+
export declare function isValidSchema(schema: T_JSONSchema): schema is T_JSONSchema;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
const enumValidator = z
|
|
3
|
+
.object({
|
|
4
|
+
enum: z.array(z.string()),
|
|
5
|
+
description: z.string().optional(),
|
|
6
|
+
default: z.string().optional(),
|
|
7
|
+
})
|
|
8
|
+
.passthrough();
|
|
9
|
+
export function isValidEnumSchema(schema) {
|
|
10
|
+
return enumValidator.safeParse(schema).success;
|
|
11
|
+
}
|
|
12
|
+
const stringValidator = z
|
|
13
|
+
.object({
|
|
14
|
+
type: z.literal('string'),
|
|
15
|
+
minLength: z.number().optional(),
|
|
16
|
+
maxLength: z.number().optional(),
|
|
17
|
+
format: z.enum(['date', 'uri']).optional(),
|
|
18
|
+
description: z.string().optional(),
|
|
19
|
+
default: z.string().optional(),
|
|
20
|
+
})
|
|
21
|
+
.passthrough();
|
|
22
|
+
export function isValidStringSchema(schema) {
|
|
23
|
+
return stringValidator.safeParse(schema).success;
|
|
24
|
+
}
|
|
25
|
+
const numberValidator = z
|
|
26
|
+
.object({
|
|
27
|
+
type: z.literal('number'),
|
|
28
|
+
minimum: z.number().optional(),
|
|
29
|
+
maximum: z.number().optional(),
|
|
30
|
+
description: z.string().optional(),
|
|
31
|
+
default: z.number().optional(),
|
|
32
|
+
})
|
|
33
|
+
.passthrough();
|
|
34
|
+
export function isValidNumberSchema(schema) {
|
|
35
|
+
return numberValidator.safeParse(schema).success;
|
|
36
|
+
}
|
|
37
|
+
const booleanValidator = z
|
|
38
|
+
.object({
|
|
39
|
+
type: z.literal('boolean'),
|
|
40
|
+
description: z.string().optional(),
|
|
41
|
+
default: z.boolean().optional(),
|
|
42
|
+
})
|
|
43
|
+
.passthrough();
|
|
44
|
+
export function isValidBooleanSchema(schema) {
|
|
45
|
+
return booleanValidator.safeParse(schema).success;
|
|
46
|
+
}
|
|
47
|
+
const arrayValidator = z
|
|
48
|
+
.object({
|
|
49
|
+
type: z.literal('array'),
|
|
50
|
+
items: z.any(),
|
|
51
|
+
description: z.string().optional(),
|
|
52
|
+
default: z.array(z.any()).optional(),
|
|
53
|
+
})
|
|
54
|
+
.passthrough();
|
|
55
|
+
export function isValidArraySchema(schema) {
|
|
56
|
+
return arrayValidator.safeParse(schema).success;
|
|
57
|
+
}
|
|
58
|
+
const objectValidator = z
|
|
59
|
+
.object({
|
|
60
|
+
type: z.literal('object'),
|
|
61
|
+
properties: z.record(z.string(), z.any()),
|
|
62
|
+
required: z.array(z.string()).optional(),
|
|
63
|
+
description: z.string().optional(),
|
|
64
|
+
default: z.record(z.string(), z.any()).optional(),
|
|
65
|
+
})
|
|
66
|
+
.passthrough();
|
|
67
|
+
export function isValidObjectSchema(schema) {
|
|
68
|
+
return objectValidator.safeParse(schema).success;
|
|
69
|
+
}
|
|
70
|
+
export function isValidSchema(schema) {
|
|
71
|
+
return (isValidEnumSchema(schema) ||
|
|
72
|
+
isValidStringSchema(schema) ||
|
|
73
|
+
isValidNumberSchema(schema) ||
|
|
74
|
+
isValidBooleanSchema(schema) ||
|
|
75
|
+
isValidObjectSchema(schema) ||
|
|
76
|
+
isValidArraySchema(schema));
|
|
77
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"root":["../src/builders.ts","../src/fromzod.ts","../src/guards.ts","../src/index.ts","../src/tozod.ts","../src/types.ts","../src/validators.ts"],"version":"5.9.
|
|
1
|
+
{"root":["../vitest.config.ts","../src/builders.ts","../src/fromzod.ts","../src/guards.ts","../src/index.ts","../src/tozod.ts","../src/types.ts","../src/validators.ts"],"version":"5.9.3"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weser/schema",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"description": "Helpers for working with Zod and JSON Schema",
|
|
5
5
|
"author": "Robin Weser <robin@weser.io>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"clean": "rimraf dist",
|
|
34
34
|
"build": "tsc -b",
|
|
35
35
|
"dev": "pnpm build -w",
|
|
36
|
-
"test": "
|
|
36
|
+
"test": "vitest run"
|
|
37
37
|
},
|
|
38
38
|
"keywords": [
|
|
39
39
|
"zod",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"zod": "*"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"ava": "^6.1.3",
|
|
50
49
|
"rimraf": "^3.0.2",
|
|
51
50
|
"typescript": "^5.4.5",
|
|
51
|
+
"vitest": "^2.1.8",
|
|
52
52
|
"zod": "4.0.0-beta.20250505T195954"
|
|
53
53
|
},
|
|
54
|
-
"gitHead": "
|
|
54
|
+
"gitHead": "490b5b37e8da7f0cac2880543874914c6f6be5d6"
|
|
55
55
|
}
|