@hyperjump/json-schema 0.18.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/LICENSE +21 -0
- package/README.md +176 -0
- package/dist/json-schema-amd.js +4710 -0
- package/dist/json-schema-amd.js.map +1 -0
- package/dist/json-schema-amd.min.js +2 -0
- package/dist/json-schema-amd.min.js.map +1 -0
- package/dist/json-schema-cjs.js +4708 -0
- package/dist/json-schema-cjs.js.map +1 -0
- package/dist/json-schema-cjs.min.js +2 -0
- package/dist/json-schema-cjs.min.js.map +1 -0
- package/dist/json-schema-esm.js +4691 -0
- package/dist/json-schema-esm.js.map +1 -0
- package/dist/json-schema-esm.min.js +2 -0
- package/dist/json-schema-esm.min.js.map +1 -0
- package/dist/json-schema-iife.js +4713 -0
- package/dist/json-schema-iife.js.map +1 -0
- package/dist/json-schema-iife.min.js +2 -0
- package/dist/json-schema-iife.min.js.map +1 -0
- package/dist/json-schema-system.js +4698 -0
- package/dist/json-schema-system.js.map +1 -0
- package/dist/json-schema-system.min.js +2 -0
- package/dist/json-schema-system.min.js.map +1 -0
- package/dist/json-schema-umd.js +4714 -0
- package/dist/json-schema-umd.js.map +1 -0
- package/dist/json-schema-umd.min.js +2 -0
- package/dist/json-schema-umd.min.js.map +1 -0
- package/lib/common.js +13 -0
- package/lib/draft-04.d.ts +40 -0
- package/lib/draft-04.js +94 -0
- package/lib/draft-06.d.ts +44 -0
- package/lib/draft-06.js +51 -0
- package/lib/draft-07.d.ts +52 -0
- package/lib/draft-07.js +55 -0
- package/lib/draft-2019-09.d.ts +62 -0
- package/lib/draft-2019-09.js +92 -0
- package/lib/draft-2020-12.d.ts +62 -0
- package/lib/draft-2020-12.js +103 -0
- package/lib/index.d.ts +28 -0
- package/lib/index.js +26 -0
- package/lib/index.mjs +18 -0
- package/lib/keywords/additionalItems.js +27 -0
- package/lib/keywords/additionalItems6.js +23 -0
- package/lib/keywords/additionalProperties.js +33 -0
- package/lib/keywords/additionalProperties6.js +28 -0
- package/lib/keywords/allOf.js +28 -0
- package/lib/keywords/anyOf.js +29 -0
- package/lib/keywords/const.js +8 -0
- package/lib/keywords/contains-minContains-maxContains.js +33 -0
- package/lib/keywords/contains.js +10 -0
- package/lib/keywords/definitions.js +15 -0
- package/lib/keywords/dependencies.js +29 -0
- package/lib/keywords/dependentRequired.js +19 -0
- package/lib/keywords/dependentSchemas.js +30 -0
- package/lib/keywords/dynamicRef.js +24 -0
- package/lib/keywords/else.js +43 -0
- package/lib/keywords/enum.js +8 -0
- package/lib/keywords/exclusiveMaximum.js +7 -0
- package/lib/keywords/exclusiveMinimum.js +7 -0
- package/lib/keywords/if.js +19 -0
- package/lib/keywords/index.js +55 -0
- package/lib/keywords/items.js +31 -0
- package/lib/keywords/items202012.js +23 -0
- package/lib/keywords/maxItems.js +7 -0
- package/lib/keywords/maxLength.js +7 -0
- package/lib/keywords/maxLength6.js +7 -0
- package/lib/keywords/maxProperties.js +7 -0
- package/lib/keywords/maximum-exclusiveMaximum.js +20 -0
- package/lib/keywords/maximum.js +7 -0
- package/lib/keywords/minItems.js +7 -0
- package/lib/keywords/minLength.js +7 -0
- package/lib/keywords/minLength6.js +7 -0
- package/lib/keywords/minProperties.js +7 -0
- package/lib/keywords/minimum-exclusiveMinimum.js +20 -0
- package/lib/keywords/minimum.js +7 -0
- package/lib/keywords/multipleOf.js +17 -0
- package/lib/keywords/not.js +7 -0
- package/lib/keywords/oneOf.js +48 -0
- package/lib/keywords/pattern.js +7 -0
- package/lib/keywords/patternProperties.js +23 -0
- package/lib/keywords/properties.js +25 -0
- package/lib/keywords/propertyNames.js +11 -0
- package/lib/keywords/ref.js +13 -0
- package/lib/keywords/required.js +10 -0
- package/lib/keywords/then.js +43 -0
- package/lib/keywords/tupleItems.js +24 -0
- package/lib/keywords/type.js +7 -0
- package/lib/keywords/unevaluatedItems.js +23 -0
- package/lib/keywords/unevaluatedProperties.js +24 -0
- package/lib/keywords/uniqueItems.js +16 -0
- package/meta/draft/2019-09/meta/applicator.js +55 -0
- package/meta/draft/2019-09/meta/content.js +17 -0
- package/meta/draft/2019-09/meta/core.js +57 -0
- package/meta/draft/2019-09/meta/format.js +14 -0
- package/meta/draft/2019-09/meta/meta-data.js +37 -0
- package/meta/draft/2019-09/meta/validation.js +98 -0
- package/meta/draft/2019-09/schema.js +42 -0
- package/meta/draft/2020-12/meta/applicator.js +49 -0
- package/meta/draft/2020-12/meta/content.js +17 -0
- package/meta/draft/2020-12/meta/core.js +57 -0
- package/meta/draft/2020-12/meta/format-annotation.js +14 -0
- package/meta/draft/2020-12/meta/format-assertion.js +14 -0
- package/meta/draft/2020-12/meta/meta-data.js +37 -0
- package/meta/draft/2020-12/meta/unevaluated.js +15 -0
- package/meta/draft/2020-12/meta/validation.js +98 -0
- package/meta/draft/2020-12/schema.js +44 -0
- package/meta/draft-04/hyper-schema.js +136 -0
- package/meta/draft-04/schema.js +149 -0
- package/meta/draft-06/schema.js +154 -0
- package/meta/draft-07/schema.js +172 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Jason Desrosiers
|
|
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,176 @@
|
|
|
1
|
+
# Hyperjump - JSON Schema Validator
|
|
2
|
+
JSON Schema Validator (JSV) is built on [JSON Schema Core](https://github.com/hyperjump-io/json-schema-core).
|
|
3
|
+
|
|
4
|
+
* Supported JSON Schema Dialects
|
|
5
|
+
* draft-04 | draft-06 | draft-07 | Draft 2019-09 | Draft 2020-12
|
|
6
|
+
* Create your own dialect with [JSC](https://github.com/hyperjump-io/json-schema-core)
|
|
7
|
+
* Schemas can reference other schemas using a different draft
|
|
8
|
+
* Supported vocabularies (new in Draft 2019-09)
|
|
9
|
+
* Draft 2019-09: core | applicator | validation | meta-data | content
|
|
10
|
+
* Draft 2020-12: core | applicator | validation | meta-data | content |
|
|
11
|
+
format-annotations
|
|
12
|
+
* Create your own keywords and vocabularies with [JSC](https://github.com/hyperjump-io/json-schema-core)
|
|
13
|
+
* Output formats
|
|
14
|
+
* FLAG, BASIC, DETAILED, VERBOSE
|
|
15
|
+
* Create your own output format with [JSC](https://github.com/hyperjump-io/json-schema-core)
|
|
16
|
+
* Load schemas from filesystem (file://), network (http(s)://), or JavaScript
|
|
17
|
+
* Build non-validation JSON-Schema based tools with [JSC](https://github.com/hyperjump-io/json-schema-core)
|
|
18
|
+
|
|
19
|
+
## Install
|
|
20
|
+
JSV includes support for node.js JavaScript (CommonJS and ES Modules),
|
|
21
|
+
TypeScript, and browsers.
|
|
22
|
+
|
|
23
|
+
### Node.js
|
|
24
|
+
```bash
|
|
25
|
+
npm install @hyperjump/json-schema
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Browser
|
|
29
|
+
When in a browser context, JSV is designed to use the browser's `fetch`
|
|
30
|
+
implementation instead of a node.js fetch clone. The Webpack bundler does this
|
|
31
|
+
properly without any extra configuration, but if you are using the Rollup
|
|
32
|
+
bundler you will need to include the `browser: true` option in your Rollup
|
|
33
|
+
configuration.
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
plugins: [
|
|
37
|
+
resolve({
|
|
38
|
+
browser: true
|
|
39
|
+
}),
|
|
40
|
+
commonjs()
|
|
41
|
+
]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Versioning
|
|
45
|
+
This project is in beta and there may be breaking changes at any time. When it's
|
|
46
|
+
stable enough, I'll publish v1.0.0 and follow semantic versioning from there on
|
|
47
|
+
out.
|
|
48
|
+
|
|
49
|
+
## Usage
|
|
50
|
+
```javascript
|
|
51
|
+
const JsonSchema = require("@hyperjump/json-schema");
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
// Example: Inline schema
|
|
55
|
+
const schemaJson = {
|
|
56
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
57
|
+
"$id": "http://example.com/schemas/string",
|
|
58
|
+
"type": "string"
|
|
59
|
+
};
|
|
60
|
+
JsonSchema.add(schemaJson);
|
|
61
|
+
const schema = await JsonSchema.get("http://example.com/schemas/string");
|
|
62
|
+
|
|
63
|
+
// Example: Fetch from the web
|
|
64
|
+
const schema = await JsonSchema.get("http://example.com/schemas/string");
|
|
65
|
+
|
|
66
|
+
// Example: Fetch from file
|
|
67
|
+
const schema = await JsonSchema.get("file:///path/to/my/schemas/string.schema.json");
|
|
68
|
+
|
|
69
|
+
// Example: Validate instance
|
|
70
|
+
const output = await JsonSchema.validate(schema, "foo");
|
|
71
|
+
if (output.valid) {
|
|
72
|
+
console.log("Instance is valid :-)");
|
|
73
|
+
} else {
|
|
74
|
+
console.log("Instance is invalid :-(");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Example: Precompile validator
|
|
78
|
+
const isString = await JsonSchema.validate(schema);
|
|
79
|
+
const output = isString("foo");
|
|
80
|
+
|
|
81
|
+
// Example: Specify output format
|
|
82
|
+
const output = await JsonSchema.validate(schema, "foo", JsonSchema.VERBOSE);
|
|
83
|
+
|
|
84
|
+
// Example: Specify meta-validation output format
|
|
85
|
+
JsonSchema.setMetaOutputFormat(JsonSchema.FLAG);
|
|
86
|
+
|
|
87
|
+
// Example: Disable meta-validation
|
|
88
|
+
JsonSchema.setShouldMetaValidate(false);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
## TypeScript
|
|
92
|
+
Although the package is written in JavaScript, type definitions are included for
|
|
93
|
+
TypeScript support. The following example shows the types you might want to
|
|
94
|
+
know.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
import JsonSchema, { InvalidSchemaError } from "@hyperjump/json-schema";
|
|
98
|
+
import type { SchemaDocument, Validator, Result, Draft202012Schema } from "@hyperjump/json-schema";
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
const schemaJson: Draft202012Schema = {
|
|
102
|
+
"$id": "https://json-schema.hyperjump.io/schema",
|
|
103
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
104
|
+
|
|
105
|
+
"type": "string"
|
|
106
|
+
};
|
|
107
|
+
JsonSchema.add(schemaJson);
|
|
108
|
+
|
|
109
|
+
const schema: SchemaDocument = await JsonSchema.get("https://json-schema.hyperjump.io/schema");
|
|
110
|
+
try {
|
|
111
|
+
const isString: Validator = await JsonSchema.validate(schema);
|
|
112
|
+
const result: Result = isString("foo");
|
|
113
|
+
console.log("isString:", result.valid);
|
|
114
|
+
} catch (error: unknown) {
|
|
115
|
+
if (error instanceof InvalidSchemaError) {
|
|
116
|
+
console.log(error.output);
|
|
117
|
+
} else {
|
|
118
|
+
console.log(error);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## API
|
|
124
|
+
* **add**: (schema: object, url?: URI, schemaVersion?: string) => SDoc
|
|
125
|
+
|
|
126
|
+
Load a schema. See [JSC - $id](https://github.com/hyperjump-io/json-schema-core#id)
|
|
127
|
+
and [JSC - $schema](https://github.com/hyperjump-io/json-schema-core#schema-1)
|
|
128
|
+
for more information.
|
|
129
|
+
* **get**: (url: URI, contextDoc?: SDoc, recursive: boolean = false) => Promise<SDoc>
|
|
130
|
+
|
|
131
|
+
Fetch a schema. Schemas can come from an HTTP request, a file, or a schema
|
|
132
|
+
that was added with `add`.
|
|
133
|
+
* **validate**: (schema: SDoc, instance: any, outputFormat: OutputFormat = FLAG) => Promise<OutputUnit>
|
|
134
|
+
|
|
135
|
+
Validate an instance against a schema. The function is curried to allow
|
|
136
|
+
compiling the schema once and applying it to multiple instances.
|
|
137
|
+
* **compile**: (schema: SDoc) => Promise<CompiledSchema>
|
|
138
|
+
|
|
139
|
+
Compile a schema to be interpreted later. A compiled schema is a JSON
|
|
140
|
+
serializable structure that can be serialized an restored for later use.
|
|
141
|
+
* **interpret**: (schema: CompiledSchema, instance: any, outputFormat: OutputFormat = FLAG) => OutputUnit
|
|
142
|
+
|
|
143
|
+
A curried function for validating an instance against a compiled schema.
|
|
144
|
+
* **setMetaOutputFormat**: (outputFormat: OutputFormat = DETAILED) => undefined
|
|
145
|
+
|
|
146
|
+
Set the output format for meta-validation. Meta-validation output is only
|
|
147
|
+
returned if meta-validation results in an error.
|
|
148
|
+
* **setShouldMetaValidate**: (isEnabled: boolean) => undefined
|
|
149
|
+
|
|
150
|
+
Enable or disable meta-validation.
|
|
151
|
+
* **OutputFormat**: [**FLAG** | **BASIC** | **DETAILED** | **VERBOSE**]
|
|
152
|
+
|
|
153
|
+
See [JSC - Output](https://github.com/hyperjump-io/json-schema-core#output)
|
|
154
|
+
for more information on output formats.
|
|
155
|
+
|
|
156
|
+
## Not (yet) Supported
|
|
157
|
+
This implementation supports all required features of JSON Schema. The following
|
|
158
|
+
optional features are not supported yet.
|
|
159
|
+
|
|
160
|
+
* The format-assertion vocabulary
|
|
161
|
+
|
|
162
|
+
## Contributing
|
|
163
|
+
|
|
164
|
+
### Tests
|
|
165
|
+
|
|
166
|
+
Run the tests
|
|
167
|
+
|
|
168
|
+
```bash
|
|
169
|
+
npm test
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Run the tests with a continuous test runner
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
npm test -- --watch
|
|
176
|
+
```
|