@weser/schema 0.0.8

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022-present Robin Weser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # @weser/context
2
+
3
+ <img alt="npm version" src="https://badge.fury.io/js/@weser%2Fcontext.svg"> <img alt="npm downloads" src="https://img.shields.io/npm/dm/@weser/context.svg"> <a href="https://bundlephobia.com/result?p=@weser/context@latest"><img alt="Bundlephobia" src="https://img.shields.io/bundlephobia/minzip/@weser/context.svg"></a>
4
+
5
+ ## Installation
6
+
7
+ ```sh
8
+ # npm
9
+ npm i --save @weser/context
10
+ # yarn
11
+ yarn add @weser/context
12
+ # pnpm
13
+ pnpm add @weser/context
14
+ ```
15
+
16
+ ## Documentation
17
+
18
+ ### `createContext<T>`
19
+
20
+ #### Parameters
21
+
22
+ | Parameter | Type | Default | Description |
23
+ | ------------ | --------- | ------- | -------------------------------------------------------- |
24
+ | defaultValue | `T` | `null` | The default value for when no provider is used |
25
+ | name |  `string` | `""` | (Optional) A name for the context used in error messages |
26
+
27
+ ```tsx
28
+ import { createContext } from '@weser/context'
29
+
30
+ type Theme = 'light' | 'dark'
31
+
32
+ const [useTheme, ThemeProvider] = createContext<Theme>('light', 'theme')
33
+
34
+ function App({ children }) {
35
+ return <ThemeProvider value="dark">{children}</ThemeProvider>
36
+ }
37
+
38
+ function Component() {
39
+ const theme = useTheme()
40
+
41
+ return (
42
+ <div style={{ backgroundColor: theme === 'dark' ? 'black' : 'white' }}>
43
+ Hello
44
+ </div>
45
+ )
46
+ }
47
+ ```
48
+
49
+ ## License
50
+
51
+ @weser/context is licensed under the [MIT License](http://opensource.org/licenses/MIT).<br>
52
+ Documentation is licensed under [Creative Common License](http://creativecommons.org/licenses/by/4.0/).<br>
53
+ Created with ♥ by [@robinweser](http://weser.io).
@@ -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';
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,3 @@
1
+ import { ZodType } from 'zod';
2
+ import { T_JSONSchema } from './types.js';
3
+ export default function fromZod(schema: ZodType): T_JSONSchema;
@@ -0,0 +1,4 @@
1
+ import z from 'zod';
2
+ export default function fromZod(schema) {
3
+ return z.toJSONSchema(schema);
4
+ }
@@ -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;
package/dist/guards.js ADDED
@@ -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,7 @@
1
+ export { default as fromZod } from './fromZod.js';
2
+ export { default as toZod } from './toZod.js';
3
+ export * from './toZod.js';
4
+ export * from './guards.js';
5
+ export * from './types.js';
6
+ export * from './validators.js';
7
+ export * from './builders.js';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export { default as fromZod } from './fromZod.js';
2
+ export { default as toZod } from './toZod.js';
3
+ export * from './toZod.js';
4
+ export * from './guards.js';
5
+ export * from './types.js';
6
+ export * from './validators.js';
7
+ export * from './builders.js';
@@ -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>>;
package/dist/toZod.js ADDED
@@ -0,0 +1,111 @@
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
+ type = type.meta({ ...meta, ...(format ? { format } : {}) });
33
+ if (description) {
34
+ type = type.describe(description);
35
+ }
36
+ if (minLength) {
37
+ type = type.min(minLength);
38
+ }
39
+ if (maxLength) {
40
+ type = type.max(maxLength);
41
+ }
42
+ if (_default) {
43
+ return type.default(_default);
44
+ }
45
+ return type;
46
+ }
47
+ export function toZodEnum({ enum: _enum, description, default: _default, ...meta }) {
48
+ let type = z.enum(_enum);
49
+ type = type.meta(meta);
50
+ if (description) {
51
+ type = type.describe(description);
52
+ }
53
+ if (_default) {
54
+ return type.default(_default);
55
+ }
56
+ return type;
57
+ }
58
+ export function toZodNumber({ minimum, maximum, description, default: _default, ...meta }) {
59
+ let type = z.number();
60
+ type = type.meta(meta);
61
+ if (description) {
62
+ type = type.describe(description);
63
+ }
64
+ if (minimum) {
65
+ type = type.min(minimum);
66
+ }
67
+ if (maximum) {
68
+ type = type.max(maximum);
69
+ }
70
+ if (_default) {
71
+ return type.default(_default);
72
+ }
73
+ return type;
74
+ }
75
+ export function toZodBoolean({ description, default: _default, ...meta }) {
76
+ let type = z.boolean();
77
+ type = type.meta(meta);
78
+ if (description) {
79
+ type = type.describe(description);
80
+ }
81
+ if (_default) {
82
+ return type.default(_default);
83
+ }
84
+ return type;
85
+ }
86
+ export function toZodObject({ properties, required, description, ...meta }) {
87
+ const props = Object.entries(properties).reduce((props, [property, schema]) => {
88
+ const type = toZod(schema);
89
+ if (type) {
90
+ props[property] = type;
91
+ }
92
+ return props;
93
+ }, {});
94
+ let type = z.object(props);
95
+ type = type.meta(meta);
96
+ if (description) {
97
+ type = type.describe(description);
98
+ }
99
+ return type;
100
+ }
101
+ export function toZodArray({ items, description, default: _default, ...meta }) {
102
+ let type = z.array(toZod(items));
103
+ type = type.meta(meta);
104
+ if (description) {
105
+ type = type.describe(description);
106
+ }
107
+ if (_default) {
108
+ return type.default(_default);
109
+ }
110
+ return type;
111
+ }
@@ -0,0 +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.2"}
@@ -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';
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;
package/dist/types.js ADDED
@@ -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
+ }
package/package.json ADDED
@@ -0,0 +1,55 @@
1
+ {
2
+ "name": "@weser/schema",
3
+ "version": "0.0.8",
4
+ "description": "Helpers for working with Zod and JSON Schema",
5
+ "author": "Robin Weser <robin@weser.io>",
6
+ "license": "MIT",
7
+ "homepage": "https://github.com/robinweser/weser.git",
8
+ "repository": "https://github.com/robinweser/weser.git",
9
+ "type": "module",
10
+ "main": "dist/index.js",
11
+ "module": "dist/index.js",
12
+ "types": "dist/index.d.ts",
13
+ "sideEffects": false,
14
+ "publishConfig": {
15
+ "access": "public"
16
+ },
17
+ "files": [
18
+ "LICENSE",
19
+ "README.md",
20
+ "dist/**"
21
+ ],
22
+ "browserslist": [
23
+ "IE >= 11",
24
+ "Firefox >= 60",
25
+ "Safari >= 11.1",
26
+ "Chrome >= 66",
27
+ "ChromeAndroid >= 66",
28
+ "iOS >= 11.3",
29
+ "Edge >= 15"
30
+ ],
31
+ "scripts": {
32
+ "setup": "pnpm build",
33
+ "clean": "rimraf dist",
34
+ "build": "tsc -b",
35
+ "dev": "pnpm build -w",
36
+ "test": "ava"
37
+ },
38
+ "keywords": [
39
+ "zod",
40
+ "json-schema",
41
+ "schema",
42
+ "conversion",
43
+ "utils"
44
+ ],
45
+ "peerDependencies": {
46
+ "zod": "*"
47
+ },
48
+ "devDependencies": {
49
+ "ava": "^6.1.3",
50
+ "rimraf": "^3.0.2",
51
+ "typescript": "^5.4.5",
52
+ "zod": "4.0.0-beta.20250505T195954"
53
+ },
54
+ "gitHead": "9dce5f658f08431e80506b022369229dfeff7dd7"
55
+ }