z-schema 9.0.0 → 10.0.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/README.md +127 -155
- package/bin/z-schema +4 -2
- package/cjs/index.d.ts +40 -9
- package/cjs/index.js +4842 -3990
- package/dist/errors.js +5 -0
- package/dist/format-validators.js +65 -0
- package/dist/json-schema-versions.js +5 -0
- package/dist/json-schema.js +11 -4
- package/dist/json-validation.js +174 -13
- package/dist/report.js +12 -3
- package/dist/schema-cache.js +23 -2
- package/dist/schema-compiler.js +29 -13
- package/dist/schema-validator.js +66 -45
- package/dist/schemas/draft-06-hyper-schema.json +132 -0
- package/dist/schemas/draft-06-links.json +43 -0
- package/dist/schemas/draft-06-schema.json +155 -0
- package/dist/types/errors.d.ts +4 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/json-schema-versions.d.ts +23 -0
- package/dist/types/json-schema.d.ts +5 -9
- package/dist/types/json-validation.d.ts +3 -3
- package/dist/types/report.d.ts +10 -3
- package/dist/types/schema-cache.d.ts +1 -1
- package/dist/types/schema-compiler.d.ts +1 -1
- package/dist/types/schema-validator.d.ts +1 -1
- package/dist/types/utils/what-is.d.ts +1 -0
- package/dist/types/z-schema-base.d.ts +1 -1
- package/dist/types/z-schema-options.d.ts +1 -1
- package/dist/types/z-schema-reader.d.ts +1 -1
- package/dist/types/z-schema-versions.d.ts +1 -0
- package/dist/types/z-schema.d.ts +10 -1
- package/dist/utils/schema-regex.js +4 -3
- package/dist/utils/what-is.js +4 -1
- package/dist/z-schema-base.js +7 -5
- package/dist/z-schema-options.js +3 -1
- package/dist/z-schema-versions.js +27 -0
- package/dist/z-schema.js +24 -7
- package/package.json +2 -2
- package/src/errors.ts +6 -0
- package/src/format-validators.ts +65 -0
- package/src/index.ts +2 -1
- package/src/json-schema-versions.ts +34 -0
- package/src/json-schema.ts +22 -16
- package/src/json-validation.ts +205 -16
- package/src/report.ts +21 -6
- package/src/schema-cache.ts +25 -3
- package/src/schema-compiler.ts +29 -14
- package/src/schema-validator.ts +128 -62
- package/src/schemas/draft-06-hyper-schema.json +133 -0
- package/src/schemas/draft-06-links.json +43 -0
- package/src/schemas/draft-06-schema.json +155 -0
- package/src/utils/schema-regex.ts +5 -3
- package/src/utils/what-is.ts +5 -1
- package/src/z-schema-base.ts +9 -6
- package/src/z-schema-options.ts +3 -2
- package/src/z-schema-reader.ts +1 -1
- package/src/z-schema-versions.ts +38 -0
- package/src/z-schema.ts +31 -11
- package/umd/ZSchema.js +5100 -4248
- package/umd/ZSchema.min.js +1 -1
package/README.md
CHANGED
|
@@ -1,241 +1,213 @@
|
|
|
1
|
-
# z-schema
|
|
1
|
+
# z-schema
|
|
2
|
+
|
|
3
|
+
JSON Schema validator for Node.js and browsers. Supports **draft-04** and **draft-06** (default).
|
|
2
4
|
|
|
3
5
|
[](https://www.npmjs.com/package/z-schema)
|
|
4
6
|
|
|
5
7
|
[](https://coveralls.io/github/zaggino/z-schema?branch=main)
|
|
6
8
|
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
- [What](#what)
|
|
10
|
-
- [Versions](#versions)
|
|
11
|
-
- [Getting started](#getting-started)
|
|
12
|
-
- [Usage](#usage)
|
|
13
|
-
- [Features](#features)
|
|
14
|
-
- [Options](#options)
|
|
15
|
-
- [Contributing](#contributing)
|
|
16
|
-
- [Contributors](#contributors)
|
|
17
|
-
|
|
18
|
-
## What
|
|
19
|
-
|
|
20
|
-
What is a JSON Schema? Find here: [https://json-schema.org/](https://json-schema.org/)
|
|
9
|
+
## Install
|
|
21
10
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- v7 - modernized version (to ESM module with Typescript) which passes all tests from JSON Schema Test Suite for draft-04
|
|
26
|
-
- v8 - by default assumes all schemas without $schema tag are draft-04, the old behaviour from v7 can be explicitly turned on by specifying `validator = ZSchema.create({ version: 'none' });`
|
|
27
|
-
- v9 - new api, `new ZSchema()` replaced by `ZSchema.create()` for initialization
|
|
11
|
+
```bash
|
|
12
|
+
npm install z-schema
|
|
13
|
+
```
|
|
28
14
|
|
|
29
|
-
|
|
15
|
+
Requires **Node.js 22** or later.
|
|
30
16
|
|
|
31
|
-
|
|
17
|
+
## Quick Start
|
|
32
18
|
|
|
33
|
-
### ESM
|
|
19
|
+
### ESM / TypeScript
|
|
34
20
|
|
|
35
21
|
```typescript
|
|
36
22
|
import ZSchema from 'z-schema';
|
|
23
|
+
|
|
37
24
|
const validator = ZSchema.create();
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
validator.validate({ name: 'Alice' }, { type: 'object', properties: { name: { type: 'string' } } });
|
|
28
|
+
console.log('Valid');
|
|
29
|
+
} catch (error) {
|
|
30
|
+
console.log('Invalid:', error.details);
|
|
31
|
+
}
|
|
38
32
|
```
|
|
39
33
|
|
|
40
|
-
###
|
|
34
|
+
### CommonJS
|
|
41
35
|
|
|
42
36
|
```javascript
|
|
43
37
|
const ZSchema = require('z-schema');
|
|
44
38
|
const validator = ZSchema.create();
|
|
45
39
|
```
|
|
46
40
|
|
|
47
|
-
### Browser
|
|
41
|
+
### Browser (UMD)
|
|
48
42
|
|
|
49
43
|
```html
|
|
50
|
-
<script
|
|
51
|
-
<script
|
|
52
|
-
|
|
44
|
+
<script src="z-schema/umd/ZSchema.min.js"></script>
|
|
45
|
+
<script>
|
|
46
|
+
const validator = ZSchema.create();
|
|
53
47
|
try {
|
|
54
|
-
validator.validate('
|
|
55
|
-
console.log('Validation passed');
|
|
48
|
+
validator.validate('hello', { type: 'string' });
|
|
56
49
|
} catch (error) {
|
|
57
|
-
console.log(
|
|
50
|
+
console.log(error.details);
|
|
58
51
|
}
|
|
59
52
|
</script>
|
|
60
53
|
```
|
|
61
54
|
|
|
62
|
-
### CLI
|
|
55
|
+
### CLI
|
|
63
56
|
|
|
64
57
|
```bash
|
|
65
58
|
npm install --global z-schema
|
|
66
|
-
z-schema --help
|
|
67
59
|
z-schema mySchema.json
|
|
68
|
-
z-schema mySchema.json
|
|
69
|
-
z-schema --strictMode mySchema.json
|
|
60
|
+
z-schema mySchema.json myData.json
|
|
61
|
+
z-schema --strictMode mySchema.json myData.json
|
|
70
62
|
```
|
|
71
63
|
|
|
72
64
|
## Usage
|
|
73
65
|
|
|
74
|
-
### Sync
|
|
66
|
+
### Sync Validation (Throw Mode)
|
|
67
|
+
|
|
68
|
+
By default, `validate` throws a `ValidateError` on failure. The error has a `details` array with structured error info.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
const validator = ZSchema.create();
|
|
75
72
|
|
|
76
|
-
```javascript
|
|
77
73
|
try {
|
|
78
74
|
validator.validate(json, schema);
|
|
79
|
-
// validation passed
|
|
80
75
|
} catch (error) {
|
|
81
|
-
// this will return a native error object with name and message
|
|
82
76
|
console.log(error.name); // 'z-schema validation error'
|
|
83
|
-
console.log(error.message); //
|
|
84
|
-
//
|
|
85
|
-
console.log(error.details); // array of detailed errors
|
|
77
|
+
console.log(error.message); // summary message
|
|
78
|
+
console.log(error.details); // array of { code, message, path, ... }
|
|
86
79
|
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Sync Validation (Safe Mode)
|
|
87
83
|
|
|
88
|
-
|
|
89
|
-
|
|
84
|
+
Use `ZSchema.create({ safe: true })` to get a result object instead of exceptions.
|
|
85
|
+
|
|
86
|
+
```typescript
|
|
87
|
+
const validator = ZSchema.create({ safe: true });
|
|
88
|
+
|
|
89
|
+
const result = validator.validate(json, schema);
|
|
90
90
|
if (!result.valid) {
|
|
91
|
-
console.log(result.
|
|
91
|
+
console.log(result.err); // ValidateError with .details
|
|
92
92
|
}
|
|
93
|
-
|
|
94
|
-
...
|
|
95
93
|
```
|
|
96
94
|
|
|
97
|
-
### Async
|
|
95
|
+
### Async Validation
|
|
98
96
|
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
- **Async validation**: User ID against a database
|
|
102
|
-
- **Async validation**: Postcode against an external service
|
|
103
|
-
- **Sync validation**: Phone number format
|
|
97
|
+
Pass `{ async: true }` to support async format validators. The `validate` method returns a Promise.
|
|
104
98
|
|
|
105
99
|
```typescript
|
|
106
|
-
|
|
107
|
-
import db from './db';
|
|
108
|
-
|
|
109
|
-
// Initialize ZSchema
|
|
110
|
-
const validator = ZSchema.create();
|
|
111
|
-
|
|
112
|
-
// Register async and sync format validators
|
|
113
|
-
validator.registerFormat('user-exists', async (input: unknown): Promise<boolean> => {
|
|
114
|
-
if (typeof input !== 'number') return false;
|
|
115
|
-
const user = await db.getUserById(input);
|
|
116
|
-
return user != null;
|
|
117
|
-
});
|
|
118
|
-
validator.registerFormat('valid-postcode', async (input: unknown): Promise<boolean> => {
|
|
119
|
-
if (typeof input !== 'string') return false;
|
|
120
|
-
const postcode = await db.getPostcode(input);
|
|
121
|
-
return postcode != null;
|
|
122
|
-
});
|
|
123
|
-
validator.registerFormat('phone-number', (input: unknown): boolean => {
|
|
124
|
-
if (typeof input !== 'string') return false;
|
|
125
|
-
const phoneRegex = /^\+?[1-9]\d{1,14}$/;
|
|
126
|
-
return phoneRegex.test(input);
|
|
127
|
-
});
|
|
100
|
+
const validator = ZSchema.create({ async: true });
|
|
128
101
|
|
|
129
|
-
// Define the JSON Schema
|
|
130
|
-
const personSchema = {
|
|
131
|
-
$schema: 'http://json-schema.org/draft-04/schema#',
|
|
132
|
-
type: 'object',
|
|
133
|
-
required: ['personId', 'address'],
|
|
134
|
-
properties: {
|
|
135
|
-
personId: {
|
|
136
|
-
type: 'number',
|
|
137
|
-
format: 'user-exists',
|
|
138
|
-
},
|
|
139
|
-
address: {
|
|
140
|
-
type: 'object',
|
|
141
|
-
required: ['postcode', 'phone'],
|
|
142
|
-
properties: {
|
|
143
|
-
postcode: {
|
|
144
|
-
type: 'string',
|
|
145
|
-
format: 'valid-postcode',
|
|
146
|
-
},
|
|
147
|
-
phone: {
|
|
148
|
-
type: 'string',
|
|
149
|
-
format: 'phone-number',
|
|
150
|
-
},
|
|
151
|
-
},
|
|
152
|
-
},
|
|
153
|
-
},
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
// Example payload
|
|
157
|
-
const payload = {
|
|
158
|
-
personId: 'user123',
|
|
159
|
-
address: {
|
|
160
|
-
postcode: 'SW1A 1AA',
|
|
161
|
-
phone: '+441234567890',
|
|
162
|
-
},
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
// Validate asynchronously
|
|
166
102
|
try {
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
console.log(
|
|
170
|
-
} catch (err) {
|
|
171
|
-
console.log('❌ Validation failed:', err);
|
|
103
|
+
await validator.validate(json, schema);
|
|
104
|
+
} catch (error) {
|
|
105
|
+
console.log(error.details);
|
|
172
106
|
}
|
|
173
107
|
|
|
174
|
-
//
|
|
175
|
-
const
|
|
176
|
-
const
|
|
177
|
-
if (
|
|
178
|
-
console.log(
|
|
179
|
-
} else {
|
|
180
|
-
console.log('❌ Validation failed:', res.err);
|
|
108
|
+
// Or combine with safe mode:
|
|
109
|
+
const safeValidator = ZSchema.create({ async: true, safe: true });
|
|
110
|
+
const result = await safeValidator.validate(json, schema);
|
|
111
|
+
if (!result.valid) {
|
|
112
|
+
console.log(result.err);
|
|
181
113
|
}
|
|
182
114
|
```
|
|
183
115
|
|
|
184
|
-
###
|
|
116
|
+
### Schema Compilation
|
|
185
117
|
|
|
186
|
-
|
|
187
|
-
Otherwise you'll get `UNRESOLVABLE_REFERENCE` error when trying to compile a schema.
|
|
118
|
+
Pre-compile schemas at startup to validate `$ref` references and cache compiled schemas.
|
|
188
119
|
|
|
189
|
-
```
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
120
|
+
```typescript
|
|
121
|
+
const validator = ZSchema.create();
|
|
122
|
+
|
|
123
|
+
const schemas = [
|
|
124
|
+
{ id: 'person', type: 'object', properties: { name: { type: 'string' } }, required: ['name'] },
|
|
125
|
+
{ id: 'team', type: 'object', properties: { lead: { $ref: 'person' } } },
|
|
126
|
+
];
|
|
193
127
|
|
|
194
128
|
try {
|
|
195
|
-
validator.
|
|
196
|
-
// This won't reach here due to unresolvable reference
|
|
129
|
+
validator.validateSchema(schemas);
|
|
197
130
|
} catch (error) {
|
|
198
|
-
|
|
199
|
-
console.log(error.details[0].code); // "UNRESOLVABLE_REFERENCE"
|
|
131
|
+
console.log('Schema errors:', error.details);
|
|
200
132
|
}
|
|
133
|
+
```
|
|
201
134
|
|
|
202
|
-
|
|
203
|
-
request(requiredUrl, function (error, response, body) {
|
|
135
|
+
### Custom Format Validators
|
|
204
136
|
|
|
205
|
-
|
|
137
|
+
Register custom format validators for sync or async checks.
|
|
206
138
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
// validation passed
|
|
210
|
-
} catch (error) {
|
|
211
|
-
// shouldn't happen after setting remote reference
|
|
212
|
-
}
|
|
139
|
+
```typescript
|
|
140
|
+
const validator = ZSchema.create();
|
|
213
141
|
|
|
214
|
-
|
|
142
|
+
// Sync format
|
|
143
|
+
validator.registerFormat('uppercase', (value: unknown): boolean => {
|
|
144
|
+
return typeof value === 'string' && value === value.toUpperCase();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Async format
|
|
148
|
+
validator.registerFormat('user-exists', async (value: unknown): Promise<boolean> => {
|
|
149
|
+
if (typeof value !== 'number') return false;
|
|
150
|
+
const user = await db.getUserById(value);
|
|
151
|
+
return user != null;
|
|
152
|
+
});
|
|
215
153
|
```
|
|
216
154
|
|
|
217
|
-
|
|
155
|
+
### Remote References
|
|
218
156
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
157
|
+
If your schemas reference remote URIs, register them before validation.
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
const validator = ZSchema.create();
|
|
161
|
+
|
|
162
|
+
// Register a remote schema manually
|
|
163
|
+
validator.setRemoteReference('http://example.com/person.json', personSchema);
|
|
164
|
+
|
|
165
|
+
// Or set a schema reader to load them automatically
|
|
166
|
+
ZSchema.setSchemaReader((uri: string) => {
|
|
167
|
+
const filePath = path.resolve(__dirname, 'schemas', uri + '.json');
|
|
168
|
+
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
|
223
169
|
});
|
|
224
170
|
```
|
|
225
171
|
|
|
172
|
+
## Version History
|
|
173
|
+
|
|
174
|
+
| Version | Changes |
|
|
175
|
+
| ------- | ------------------------------------------------------------------------------------------------- |
|
|
176
|
+
| **v10** | Default version is **draft-06**. Implemented draft-06 tests from JSON Schema Test suite. |
|
|
177
|
+
| **v9** | New factory API: `ZSchema.create()` replaces `new ZSchema()`. Default draft is **draft-06**. |
|
|
178
|
+
| **v8** | Schemas without `$schema` default to draft-04. Use `{ version: 'none' }` for the old v7 behavior. |
|
|
179
|
+
| **v7** | Rewritten in TypeScript/ESM. Passes all JSON Schema Test Suite tests for draft-04. |
|
|
180
|
+
| **v6** | Legacy version. Draft-04 support. |
|
|
181
|
+
|
|
226
182
|
## Features
|
|
227
183
|
|
|
228
|
-
See [
|
|
184
|
+
See [docs/features.md](docs/features.md) for the full feature list.
|
|
229
185
|
|
|
230
186
|
## Options
|
|
231
187
|
|
|
232
|
-
See [
|
|
188
|
+
See [docs/options.md](docs/options.md) for all constructor and per-call options.
|
|
189
|
+
|
|
190
|
+
## Documentation
|
|
191
|
+
|
|
192
|
+
| Document | Description |
|
|
193
|
+
| -------------------------------------------- | ------------------------------------------------------------------------------------- |
|
|
194
|
+
| [docs/usage.md](docs/usage.md) | Detailed usage guide with all validation modes, error handling, and advanced features |
|
|
195
|
+
| [docs/options.md](docs/options.md) | Constructor options and per-call validation options |
|
|
196
|
+
| [docs/features.md](docs/features.md) | Feature catalog with examples |
|
|
197
|
+
| [docs/architecture.md](docs/architecture.md) | Internal architecture, module structure, and public API reference |
|
|
198
|
+
| [docs/conventions.md](docs/conventions.md) | Code style, naming, and formatting conventions |
|
|
199
|
+
| [docs/testing.md](docs/testing.md) | Test framework, running tests, and writing new tests |
|
|
200
|
+
| [docs/contributing.md](docs/contributing.md) | PR workflow and contribution guidelines |
|
|
233
201
|
|
|
234
202
|
## Contributing
|
|
235
203
|
|
|
236
|
-
|
|
204
|
+
This repository uses submodules. Clone with:
|
|
205
|
+
|
|
206
|
+
```bash
|
|
207
|
+
git clone --recursive https://github.com/zaggino/z-schema.git
|
|
208
|
+
```
|
|
237
209
|
|
|
238
|
-
|
|
210
|
+
See [docs/contributing.md](docs/contributing.md) for the full contribution guide.
|
|
239
211
|
|
|
240
212
|
## Contributors
|
|
241
213
|
|
package/bin/z-schema
CHANGED
|
@@ -44,10 +44,11 @@ program
|
|
|
44
44
|
|
|
45
45
|
var options = {};
|
|
46
46
|
var defaultOptions = ZSchema.getDefaultOptions();
|
|
47
|
+
var programOptions = program._optionValues; // { breakOnFirstError: true }
|
|
47
48
|
|
|
48
49
|
for (var key in defaultOptions) {
|
|
49
|
-
if (
|
|
50
|
-
options[key] =
|
|
50
|
+
if (programOptions[key] != null) {
|
|
51
|
+
options[key] = programOptions[key];
|
|
51
52
|
}
|
|
52
53
|
}
|
|
53
54
|
|
|
@@ -75,6 +76,7 @@ function readJson(fileName) {
|
|
|
75
76
|
var validator = ZSchema.create(options);
|
|
76
77
|
var schemaFilePath = program.args.shift();
|
|
77
78
|
var schema = readJson(schemaFilePath);
|
|
79
|
+
schema.id = schemaFilePath;
|
|
78
80
|
|
|
79
81
|
function validateWithAutomaticDownloads(filePath, data, schema, callback) {
|
|
80
82
|
var lastResult;
|
package/cjs/index.d.ts
CHANGED
|
@@ -57,6 +57,11 @@ interface SchemaErrorDetail {
|
|
|
57
57
|
* Example: "#/projects/1"
|
|
58
58
|
*/
|
|
59
59
|
path: string | Array<string | number>;
|
|
60
|
+
/**
|
|
61
|
+
* A JSON path indicating the location in the schema where the constraint is defined.
|
|
62
|
+
* Example: ["properties", "name", "type"]
|
|
63
|
+
*/
|
|
64
|
+
schemaPath?: Array<string | number>;
|
|
60
65
|
/**
|
|
61
66
|
* The schema rule description, which is included for certain errors where
|
|
62
67
|
* this information is useful (e.g. to describe a constraint).
|
|
@@ -90,6 +95,7 @@ declare class Report {
|
|
|
90
95
|
errors: SchemaErrorDetail[];
|
|
91
96
|
json?: unknown;
|
|
92
97
|
path: Array<number | string>;
|
|
98
|
+
schemaPath: Array<number | string>;
|
|
93
99
|
rootSchema?: JsonSchemaInternal;
|
|
94
100
|
parentReport?: Report;
|
|
95
101
|
options: ZSchemaOptions;
|
|
@@ -103,11 +109,12 @@ declare class Report {
|
|
|
103
109
|
getAncestor(id: string): Report | undefined;
|
|
104
110
|
processAsyncTasks(timeout: number | undefined, callback: ValidateCallback): void;
|
|
105
111
|
getPath(returnPathAsString?: boolean): string | (string | number)[];
|
|
112
|
+
getSchemaPath(): Array<string | number>;
|
|
106
113
|
getSchemaId(): string | undefined;
|
|
107
114
|
hasError(errCode: string, errParams: Array<any>): boolean;
|
|
108
|
-
addError(errCode: ErrorCode, errParams?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema, keyword?: keyof JsonSchema): void;
|
|
115
|
+
addError(errCode: ErrorCode, errParams?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema | boolean, keyword?: keyof JsonSchema): void;
|
|
109
116
|
getJson(): unknown;
|
|
110
|
-
addCustomError(errorCode: ErrorCode, errorMessage: string, params?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema, keyword?: keyof JsonSchema): void;
|
|
117
|
+
addCustomError(errorCode: ErrorCode, errorMessage: string, params?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema | boolean, keyword?: keyof JsonSchema): void;
|
|
111
118
|
}
|
|
112
119
|
|
|
113
120
|
type ErrorCode = keyof typeof Errors;
|
|
@@ -155,6 +162,10 @@ declare const Errors: {
|
|
|
155
162
|
ASYNC_TIMEOUT: string;
|
|
156
163
|
PARENT_SCHEMA_VALIDATION_FAILED: string;
|
|
157
164
|
REMOTE_NOT_VALID: string;
|
|
165
|
+
SCHEMA_IS_FALSE: string;
|
|
166
|
+
CONST: string;
|
|
167
|
+
CONTAINS: string;
|
|
168
|
+
PROPERTY_NAMES: string;
|
|
158
169
|
};
|
|
159
170
|
declare class ValidateError extends Error {
|
|
160
171
|
name: string;
|
|
@@ -237,11 +248,10 @@ declare class SchemaCompiler {
|
|
|
237
248
|
compileArrayOfSchemasLoop(mainReport: Report, arr: JsonSchemaInternal[]): number;
|
|
238
249
|
}
|
|
239
250
|
|
|
240
|
-
|
|
241
|
-
interface JsonSchema {
|
|
251
|
+
interface JsonSchemaCommon {
|
|
242
252
|
$ref?: string;
|
|
243
|
-
id?: string;
|
|
244
253
|
$schema?: string;
|
|
254
|
+
id?: string;
|
|
245
255
|
title?: string;
|
|
246
256
|
description?: string;
|
|
247
257
|
default?: unknown;
|
|
@@ -249,12 +259,9 @@ interface JsonSchema {
|
|
|
249
259
|
type?: string | string[];
|
|
250
260
|
properties?: Record<string, JsonSchema>;
|
|
251
261
|
patternProperties?: Record<string, JsonSchema>;
|
|
252
|
-
dependencies?: Record<string, string[]>;
|
|
253
262
|
multipleOf?: number;
|
|
254
263
|
minimum?: number;
|
|
255
|
-
exclusiveMinimum?: boolean;
|
|
256
264
|
maximum?: number;
|
|
257
|
-
exclusiveMaximum?: boolean;
|
|
258
265
|
minLength?: number;
|
|
259
266
|
maxLength?: number;
|
|
260
267
|
pattern?: string;
|
|
@@ -267,6 +274,7 @@ interface JsonSchema {
|
|
|
267
274
|
maxProperties?: number;
|
|
268
275
|
required?: string[];
|
|
269
276
|
additionalProperties?: boolean | JsonSchema;
|
|
277
|
+
dependencies?: Record<string, string[] | JsonSchema>;
|
|
270
278
|
enum?: Array<unknown>;
|
|
271
279
|
format?: string;
|
|
272
280
|
allOf?: JsonSchema[];
|
|
@@ -284,12 +292,29 @@ interface ZSchemaInternalProperties {
|
|
|
284
292
|
__$validationOptions?: ZSchemaOptions;
|
|
285
293
|
__$visited?: boolean;
|
|
286
294
|
}
|
|
287
|
-
|
|
295
|
+
|
|
296
|
+
type JsonSchemaVersion = 'draft-04' | 'draft-06';
|
|
297
|
+
type JsonSchema = JsonSchemaDraft4 | JsonSchemaDraft6;
|
|
298
|
+
type JsonSchemaInternal = JsonSchema & ZSchemaInternalProperties;
|
|
299
|
+
interface JsonSchemaDraft4 extends JsonSchemaCommon {
|
|
300
|
+
exclusiveMaximum?: boolean;
|
|
301
|
+
exclusiveMinimum?: boolean;
|
|
302
|
+
}
|
|
303
|
+
interface JsonSchemaDraft6 extends JsonSchemaCommon {
|
|
304
|
+
$id?: string;
|
|
305
|
+
examples?: unknown[];
|
|
306
|
+
const?: unknown;
|
|
307
|
+
contains?: JsonSchema;
|
|
308
|
+
propertyNames?: JsonSchema;
|
|
309
|
+
exclusiveMaximum?: number;
|
|
310
|
+
exclusiveMinimum?: number;
|
|
288
311
|
}
|
|
289
312
|
|
|
290
313
|
type SchemaReader = (uri: string) => JsonSchema;
|
|
291
314
|
|
|
292
315
|
declare class ZSchema extends ZSchemaBase {
|
|
316
|
+
/** @deprecated Use ZSchema.create() instead. */
|
|
317
|
+
private constructor();
|
|
293
318
|
static registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
|
|
294
319
|
static unregisterFormat(name: string): void;
|
|
295
320
|
static getRegisteredFormats(): string[];
|
|
@@ -318,14 +343,20 @@ declare class ZSchema extends ZSchemaBase {
|
|
|
318
343
|
validateSchemaSafe(schemaOrArr: JsonSchema | JsonSchema[]): ValidateResponse;
|
|
319
344
|
}
|
|
320
345
|
declare class ZSchemaSafe extends ZSchemaBase {
|
|
346
|
+
/** @deprecated Use ZSchema.create() instead. */
|
|
347
|
+
private constructor();
|
|
321
348
|
validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): ValidateResponse;
|
|
322
349
|
validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): ValidateResponse;
|
|
323
350
|
}
|
|
324
351
|
declare class ZSchemaAsync extends ZSchemaBase {
|
|
352
|
+
/** @deprecated Use ZSchema.create() instead. */
|
|
353
|
+
private constructor();
|
|
325
354
|
validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<true>;
|
|
326
355
|
validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): true;
|
|
327
356
|
}
|
|
328
357
|
declare class ZSchemaAsyncSafe extends ZSchemaBase {
|
|
358
|
+
/** @deprecated Use ZSchema.create() instead. */
|
|
359
|
+
private constructor();
|
|
329
360
|
validate(json: unknown, schema: JsonSchema | string, options?: ValidateOptions): Promise<ValidateResponse>;
|
|
330
361
|
validateSchema(schemaOrArr: JsonSchema | JsonSchema[]): ValidateResponse;
|
|
331
362
|
}
|