xsschema 0.1.0-beta.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/LICENSE.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Moeru AI
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.
@@ -0,0 +1,178 @@
1
+ import { StandardSchemaV1 } from '@standard-schema/spec';
2
+ export { StandardSchemaV1 as Schema } from '@standard-schema/spec';
3
+
4
+ // ==================================================================================================
5
+ // JSON Schema Draft 07
6
+ // ==================================================================================================
7
+ // https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
8
+ // --------------------------------------------------------------------------------------------------
9
+
10
+ /**
11
+ * Primitive type
12
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
13
+ */
14
+ type JSONSchema7TypeName =
15
+ | "string" //
16
+ | "number"
17
+ | "integer"
18
+ | "boolean"
19
+ | "object"
20
+ | "array"
21
+ | "null";
22
+
23
+ /**
24
+ * Primitive type
25
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1.1
26
+ */
27
+ type JSONSchema7Type =
28
+ | string //
29
+ | number
30
+ | boolean
31
+ | JSONSchema7Object
32
+ | JSONSchema7Array
33
+ | null;
34
+
35
+ // Workaround for infinite type recursion
36
+ interface JSONSchema7Object {
37
+ [key: string]: JSONSchema7Type;
38
+ }
39
+
40
+ // Workaround for infinite type recursion
41
+ // https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
42
+ interface JSONSchema7Array extends Array<JSONSchema7Type> {}
43
+
44
+ /**
45
+ * Meta schema
46
+ *
47
+ * Recommended values:
48
+ * - 'http://json-schema.org/schema#'
49
+ * - 'http://json-schema.org/hyper-schema#'
50
+ * - 'http://json-schema.org/draft-07/schema#'
51
+ * - 'http://json-schema.org/draft-07/hyper-schema#'
52
+ *
53
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-5
54
+ */
55
+ type JSONSchema7Version = string;
56
+
57
+ /**
58
+ * JSON Schema v7
59
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01
60
+ */
61
+ type JSONSchema7Definition = JSONSchema7 | boolean;
62
+ interface JSONSchema7 {
63
+ $id?: string | undefined;
64
+ $ref?: string | undefined;
65
+ $schema?: JSONSchema7Version | undefined;
66
+ $comment?: string | undefined;
67
+
68
+ /**
69
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-8.2.4
70
+ * @see https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#appendix-A
71
+ */
72
+ $defs?: {
73
+ [key: string]: JSONSchema7Definition;
74
+ } | undefined;
75
+
76
+ /**
77
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.1
78
+ */
79
+ type?: JSONSchema7TypeName | JSONSchema7TypeName[] | undefined;
80
+ enum?: JSONSchema7Type[] | undefined;
81
+ const?: JSONSchema7Type | undefined;
82
+
83
+ /**
84
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.2
85
+ */
86
+ multipleOf?: number | undefined;
87
+ maximum?: number | undefined;
88
+ exclusiveMaximum?: number | undefined;
89
+ minimum?: number | undefined;
90
+ exclusiveMinimum?: number | undefined;
91
+
92
+ /**
93
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.3
94
+ */
95
+ maxLength?: number | undefined;
96
+ minLength?: number | undefined;
97
+ pattern?: string | undefined;
98
+
99
+ /**
100
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.4
101
+ */
102
+ items?: JSONSchema7Definition | JSONSchema7Definition[] | undefined;
103
+ additionalItems?: JSONSchema7Definition | undefined;
104
+ maxItems?: number | undefined;
105
+ minItems?: number | undefined;
106
+ uniqueItems?: boolean | undefined;
107
+ contains?: JSONSchema7Definition | undefined;
108
+
109
+ /**
110
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.5
111
+ */
112
+ maxProperties?: number | undefined;
113
+ minProperties?: number | undefined;
114
+ required?: string[] | undefined;
115
+ properties?: {
116
+ [key: string]: JSONSchema7Definition;
117
+ } | undefined;
118
+ patternProperties?: {
119
+ [key: string]: JSONSchema7Definition;
120
+ } | undefined;
121
+ additionalProperties?: JSONSchema7Definition | undefined;
122
+ dependencies?: {
123
+ [key: string]: JSONSchema7Definition | string[];
124
+ } | undefined;
125
+ propertyNames?: JSONSchema7Definition | undefined;
126
+
127
+ /**
128
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.6
129
+ */
130
+ if?: JSONSchema7Definition | undefined;
131
+ then?: JSONSchema7Definition | undefined;
132
+ else?: JSONSchema7Definition | undefined;
133
+
134
+ /**
135
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-6.7
136
+ */
137
+ allOf?: JSONSchema7Definition[] | undefined;
138
+ anyOf?: JSONSchema7Definition[] | undefined;
139
+ oneOf?: JSONSchema7Definition[] | undefined;
140
+ not?: JSONSchema7Definition | undefined;
141
+
142
+ /**
143
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-7
144
+ */
145
+ format?: string | undefined;
146
+
147
+ /**
148
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-8
149
+ */
150
+ contentMediaType?: string | undefined;
151
+ contentEncoding?: string | undefined;
152
+
153
+ /**
154
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-9
155
+ */
156
+ definitions?: {
157
+ [key: string]: JSONSchema7Definition;
158
+ } | undefined;
159
+
160
+ /**
161
+ * @see https://tools.ietf.org/html/draft-handrews-json-schema-validation-01#section-10
162
+ */
163
+ title?: string | undefined;
164
+ description?: string | undefined;
165
+ default?: JSONSchema7Type | undefined;
166
+ readOnly?: boolean | undefined;
167
+ writeOnly?: boolean | undefined;
168
+ examples?: JSONSchema7Type | undefined;
169
+ }
170
+
171
+ declare const toJsonSchema: (schema: StandardSchemaV1) => Promise<JSONSchema7>;
172
+
173
+ type Infer<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>;
174
+ type InferIn<T extends StandardSchemaV1> = StandardSchemaV1.InferInput<T>;
175
+
176
+ declare const validate: <T extends StandardSchemaV1>(schema: T, input: StandardSchemaV1.InferInput<T>) => Promise<StandardSchemaV1.InferOutput<T>>;
177
+
178
+ export { type Infer, type InferIn, type JSONSchema7 as JSONSchema, type JSONSchema7 as JsonSchema, toJsonSchema as toJSONSchema, toJsonSchema, validate };
package/dist/index.js ADDED
@@ -0,0 +1,22 @@
1
+ const toJsonSchema = async (schema) => {
2
+ switch (schema["~standard"].vendor) {
3
+ case "valibot":
4
+ return (await import('./valibot-wA2mWK4s.js')).toJsonSchema(schema);
5
+ case "zod":
6
+ return (await import('./zod-Cz57Gws5.js')).toJsonSchema(schema);
7
+ default:
8
+ throw new Error(`xsschema: Unsupported schema vendor ${schema["~standard"].vendor}`);
9
+ }
10
+ };
11
+
12
+ const validate = async (schema, input) => {
13
+ let result = schema["~standard"].validate(input);
14
+ if (result instanceof Promise)
15
+ result = await result;
16
+ if (result.issues) {
17
+ throw new Error(JSON.stringify(result.issues, null, 2));
18
+ }
19
+ return result.value;
20
+ };
21
+
22
+ export { toJsonSchema as toJSONSchema, toJsonSchema, validate };
@@ -0,0 +1 @@
1
+ export { toJsonSchema } from '@valibot/to-json-schema';
@@ -0,0 +1 @@
1
+ export { zodToJsonSchema as toJsonSchema } from 'zod-to-json-schema';
package/package.json ADDED
@@ -0,0 +1,56 @@
1
+ {
2
+ "name": "xsschema",
3
+ "type": "module",
4
+ "version": "0.1.0-beta.1",
5
+ "description": "extra-small, Standard Schema-based alternative to typeschema.",
6
+ "author": "Moeru AI",
7
+ "license": "MIT",
8
+ "homepage": "https://xsai.js.org",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/moeru-ai/xsai.git",
12
+ "directory": "packages/xsschema"
13
+ },
14
+ "bugs": "https://github.com/moeru-ai/xsai/issues",
15
+ "keywords": [
16
+ "xsai",
17
+ "standard-schema",
18
+ "typeschema"
19
+ ],
20
+ "sideEffects": false,
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ }
26
+ },
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "files": [
30
+ "dist"
31
+ ],
32
+ "peerDependencies": {
33
+ "@valibot/to-json-schema": "^1.0.0-beta.5",
34
+ "zod-to-json-schema": "^3.24.1"
35
+ },
36
+ "peerDependenciesMeta": {
37
+ "@valibot/to-json-schema": {
38
+ "optional": true
39
+ },
40
+ "zod-to-json-schema": {
41
+ "optional": true
42
+ }
43
+ },
44
+ "dependencies": {
45
+ "@standard-schema/spec": "^1.0.0"
46
+ },
47
+ "devDependencies": {
48
+ "@types/json-schema": "^7.0.15",
49
+ "@valibot/to-json-schema": "^1.0.0-beta.5",
50
+ "zod-to-json-schema": "^3.24.1"
51
+ },
52
+ "scripts": {
53
+ "build": "pkgroll",
54
+ "build:watch": "pkgroll --watch"
55
+ }
56
+ }