@bedrockio/yada 1.5.0 → 1.5.2
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/CHANGELOG.md +9 -0
- package/README.md +28 -7
- package/dist/cjs/Schema.js +61 -48
- package/dist/cjs/TypeSchema.js +2 -2
- package/dist/cjs/array.js +4 -4
- package/dist/cjs/date.js +2 -2
- package/dist/cjs/number.js +2 -2
- package/dist/cjs/object.js +16 -7
- package/dist/cjs/string.js +2 -2
- package/dist/cjs/tuple.js +3 -3
- package/package.json +1 -1
- package/src/Schema.js +58 -45
- package/src/TypeSchema.js +2 -2
- package/src/array.js +4 -4
- package/src/date.js +2 -2
- package/src/number.js +2 -2
- package/src/object.js +15 -6
- package/src/string.js +2 -2
- package/src/tuple.js +3 -3
- package/types/Schema.d.ts +13 -2
- package/types/Schema.d.ts.map +1 -1
- package/types/object.d.ts +3 -3
- package/types/object.d.ts.map +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,12 @@
|
|
|
1
|
+
## 1.5.2
|
|
2
|
+
|
|
3
|
+
- Removed boolean requried fields for further schema adherence.
|
|
4
|
+
- Added `toJSON` as main serialization method and moved `toOpenApi` to an alias.
|
|
5
|
+
|
|
6
|
+
## 1.5.1
|
|
7
|
+
|
|
8
|
+
- Require more strict adherence to JSON Schema.
|
|
9
|
+
|
|
1
10
|
## 1.5.0
|
|
2
11
|
|
|
3
12
|
- Calendar date validator moved to string as it cannot work when coerced to a
|
package/README.md
CHANGED
|
@@ -888,26 +888,38 @@ const schema = yd.custom((val) => {
|
|
|
888
888
|
});
|
|
889
889
|
```
|
|
890
890
|
|
|
891
|
-
##
|
|
891
|
+
## JSON Schema
|
|
892
892
|
|
|
893
|
-
One of the benefits of Yada is built-in
|
|
894
|
-
describe itself in
|
|
893
|
+
One of the benefits of Yada is built-in [JSON Schema](https://json-schema.org/)
|
|
894
|
+
integration. Any schema can describe itself in JSON Schema format including
|
|
895
|
+
complex nested schemas.
|
|
895
896
|
|
|
896
897
|
```js
|
|
897
|
-
yd.string().allow('foo', 'bar').
|
|
898
|
+
yd.string().allow('foo', 'bar').toJSON();
|
|
898
899
|
// {
|
|
899
900
|
// type: 'string',
|
|
900
901
|
// enum: ['foo', 'bar'],
|
|
901
902
|
// }
|
|
902
903
|
```
|
|
903
904
|
|
|
904
|
-
|
|
905
|
+
This also means schemas work with JSON helpers:
|
|
906
|
+
|
|
907
|
+
```js
|
|
908
|
+
const schema = yd.string().allow('foo', 'bar');
|
|
909
|
+
JSON.stringify(schema, null, 2);
|
|
910
|
+
// {
|
|
911
|
+
// type: 'string',
|
|
912
|
+
// enum: ['foo', 'bar'],
|
|
913
|
+
// }
|
|
914
|
+
```
|
|
915
|
+
|
|
916
|
+
The `tag` method allows tagging custom fields:
|
|
905
917
|
|
|
906
918
|
```js
|
|
907
919
|
yd.string().tag({
|
|
908
920
|
description: 'my description!',
|
|
909
921
|
x-field: 'my custom field!',
|
|
910
|
-
}).
|
|
922
|
+
}).toJSON();
|
|
911
923
|
// {
|
|
912
924
|
// type: 'string',
|
|
913
925
|
// description: 'my description!',
|
|
@@ -918,13 +930,22 @@ yd.string().tag({
|
|
|
918
930
|
The `description` method is a shortcut:
|
|
919
931
|
|
|
920
932
|
```js
|
|
921
|
-
yd.string().description('my description!').
|
|
933
|
+
yd.string().description('my description!').toJSON();
|
|
922
934
|
// {
|
|
923
935
|
// type: 'string',
|
|
924
936
|
// description: 'my description!',
|
|
925
937
|
// }
|
|
926
938
|
```
|
|
927
939
|
|
|
940
|
+
The method `toOpenApi` is provided as an alias:
|
|
941
|
+
|
|
942
|
+
```js
|
|
943
|
+
yd.string().toOpenApi();
|
|
944
|
+
// {
|
|
945
|
+
// type: 'string',
|
|
946
|
+
// }
|
|
947
|
+
```
|
|
948
|
+
|
|
928
949
|
## Utils
|
|
929
950
|
|
|
930
951
|
Basic utility methods:
|
package/dist/cjs/Schema.js
CHANGED
|
@@ -213,23 +213,36 @@ class Schema {
|
|
|
213
213
|
merged.assertions = [...this.assertions, ...schema.assertions];
|
|
214
214
|
return merged;
|
|
215
215
|
}
|
|
216
|
-
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Exports the schema in [JSON Schema](https://json-schema.org/) format.
|
|
219
|
+
* Note that this may not represent the schema in its enitrety. Specifically,
|
|
220
|
+
* custom (code-based) assertions will not be output.
|
|
221
|
+
* @param {Object} [extra]
|
|
222
|
+
*/
|
|
223
|
+
toJSON(extra) {
|
|
217
224
|
const {
|
|
218
|
-
required,
|
|
219
225
|
format,
|
|
220
226
|
tags
|
|
221
227
|
} = this.meta;
|
|
222
228
|
return {
|
|
223
|
-
required,
|
|
224
229
|
format,
|
|
225
230
|
...tags,
|
|
226
231
|
...this.getAnyType(),
|
|
227
232
|
...this.getDefault(),
|
|
228
233
|
...this.getNullable(),
|
|
229
|
-
...this.
|
|
234
|
+
...this.getEnum(),
|
|
230
235
|
...this.expandExtra(extra)
|
|
231
236
|
};
|
|
232
237
|
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Exports the schema in [JSON Schema](https://json-schema.org/) format.
|
|
241
|
+
* @alias toJSON.
|
|
242
|
+
*/
|
|
243
|
+
toOpenApi(...extra) {
|
|
244
|
+
return this.toJSON(extra);
|
|
245
|
+
}
|
|
233
246
|
getAnyType() {
|
|
234
247
|
const {
|
|
235
248
|
type,
|
|
@@ -263,6 +276,49 @@ class Schema {
|
|
|
263
276
|
};
|
|
264
277
|
}
|
|
265
278
|
}
|
|
279
|
+
getEnum() {
|
|
280
|
+
const {
|
|
281
|
+
enum: allowed
|
|
282
|
+
} = this.meta;
|
|
283
|
+
if (allowed?.length) {
|
|
284
|
+
const type = typeof allowed[0];
|
|
285
|
+
const allowEnum = allowed.every(entry => {
|
|
286
|
+
const entryType = typeof entry;
|
|
287
|
+
return entryType !== 'object' && entryType === type;
|
|
288
|
+
});
|
|
289
|
+
if (allowEnum) {
|
|
290
|
+
return {
|
|
291
|
+
type,
|
|
292
|
+
enum: allowed
|
|
293
|
+
};
|
|
294
|
+
} else {
|
|
295
|
+
const oneOf = [];
|
|
296
|
+
for (let entry of allowed) {
|
|
297
|
+
if (isSchema(entry)) {
|
|
298
|
+
oneOf.push(entry.toJSON());
|
|
299
|
+
} else {
|
|
300
|
+
const type = typeof entry;
|
|
301
|
+
let forType = oneOf.find(el => {
|
|
302
|
+
return el.type === type;
|
|
303
|
+
});
|
|
304
|
+
if (!forType) {
|
|
305
|
+
forType = {
|
|
306
|
+
type,
|
|
307
|
+
enum: []
|
|
308
|
+
};
|
|
309
|
+
oneOf.push(forType);
|
|
310
|
+
}
|
|
311
|
+
if (forType.enum) {
|
|
312
|
+
forType.enum.push(entry);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return {
|
|
317
|
+
oneOf
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
266
322
|
expandExtra(extra = {}) {
|
|
267
323
|
const {
|
|
268
324
|
tag,
|
|
@@ -274,7 +330,7 @@ class Schema {
|
|
|
274
330
|
return rest;
|
|
275
331
|
}
|
|
276
332
|
inspect() {
|
|
277
|
-
return JSON.stringify(this.
|
|
333
|
+
return JSON.stringify(this.toJSON(), null, 2);
|
|
278
334
|
}
|
|
279
335
|
get() {
|
|
280
336
|
const {
|
|
@@ -391,49 +447,6 @@ class Schema {
|
|
|
391
447
|
}
|
|
392
448
|
return value;
|
|
393
449
|
}
|
|
394
|
-
enumToOpenApi() {
|
|
395
|
-
const {
|
|
396
|
-
enum: allowed
|
|
397
|
-
} = this.meta;
|
|
398
|
-
if (allowed?.length) {
|
|
399
|
-
const type = typeof allowed[0];
|
|
400
|
-
const allowEnum = allowed.every(entry => {
|
|
401
|
-
const entryType = typeof entry;
|
|
402
|
-
return entryType !== 'object' && entryType === type;
|
|
403
|
-
});
|
|
404
|
-
if (allowEnum) {
|
|
405
|
-
return {
|
|
406
|
-
type,
|
|
407
|
-
enum: allowed
|
|
408
|
-
};
|
|
409
|
-
} else {
|
|
410
|
-
const oneOf = [];
|
|
411
|
-
for (let entry of allowed) {
|
|
412
|
-
if (isSchema(entry)) {
|
|
413
|
-
oneOf.push(entry.toOpenApi());
|
|
414
|
-
} else {
|
|
415
|
-
const type = typeof entry;
|
|
416
|
-
let forType = oneOf.find(el => {
|
|
417
|
-
return el.type === type;
|
|
418
|
-
});
|
|
419
|
-
if (!forType) {
|
|
420
|
-
forType = {
|
|
421
|
-
type,
|
|
422
|
-
enum: []
|
|
423
|
-
};
|
|
424
|
-
oneOf.push(forType);
|
|
425
|
-
}
|
|
426
|
-
if (forType.enum) {
|
|
427
|
-
forType.enum.push(entry);
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
return {
|
|
432
|
-
oneOf
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
450
|
}
|
|
438
451
|
exports.default = Schema;
|
|
439
452
|
function assertTypes(options) {
|
package/dist/cjs/TypeSchema.js
CHANGED
package/dist/cjs/array.js
CHANGED
|
@@ -111,7 +111,7 @@ class ArraySchema extends _TypeSchema.default {
|
|
|
111
111
|
toString() {
|
|
112
112
|
return 'array';
|
|
113
113
|
}
|
|
114
|
-
|
|
114
|
+
toJSON(extra) {
|
|
115
115
|
let other;
|
|
116
116
|
const {
|
|
117
117
|
schemas
|
|
@@ -119,16 +119,16 @@ class ArraySchema extends _TypeSchema.default {
|
|
|
119
119
|
if (schemas.length > 1) {
|
|
120
120
|
other = {
|
|
121
121
|
oneOf: schemas.map(schema => {
|
|
122
|
-
return schema.
|
|
122
|
+
return schema.toJSON();
|
|
123
123
|
})
|
|
124
124
|
};
|
|
125
125
|
} else if (schemas.length === 1) {
|
|
126
126
|
other = {
|
|
127
|
-
items: schemas[0].
|
|
127
|
+
items: schemas[0].toJSON()
|
|
128
128
|
};
|
|
129
129
|
}
|
|
130
130
|
return {
|
|
131
|
-
...super.
|
|
131
|
+
...super.toJSON(extra),
|
|
132
132
|
...other,
|
|
133
133
|
type: 'array'
|
|
134
134
|
};
|
package/dist/cjs/date.js
CHANGED
|
@@ -142,12 +142,12 @@ class DateSchema extends _Schema.default {
|
|
|
142
142
|
toString() {
|
|
143
143
|
return 'date';
|
|
144
144
|
}
|
|
145
|
-
|
|
145
|
+
toJSON(extra) {
|
|
146
146
|
const {
|
|
147
147
|
format
|
|
148
148
|
} = this.meta;
|
|
149
149
|
return {
|
|
150
|
-
...super.
|
|
150
|
+
...super.toJSON(extra),
|
|
151
151
|
type: format.includes('timestamp') ? 'number' : 'string'
|
|
152
152
|
};
|
|
153
153
|
}
|
package/dist/cjs/number.js
CHANGED
|
@@ -79,14 +79,14 @@ class NumberSchema extends _TypeSchema.default {
|
|
|
79
79
|
|
|
80
80
|
// Private
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
toJSON(extra) {
|
|
83
83
|
const {
|
|
84
84
|
min,
|
|
85
85
|
max,
|
|
86
86
|
multiple
|
|
87
87
|
} = this.meta;
|
|
88
88
|
return {
|
|
89
|
-
...super.
|
|
89
|
+
...super.toJSON(extra),
|
|
90
90
|
...(min != null && {
|
|
91
91
|
minimum: min
|
|
92
92
|
}),
|
package/dist/cjs/object.js
CHANGED
|
@@ -213,9 +213,9 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
213
213
|
/**
|
|
214
214
|
* Returns the schema's fields as an object allowing them
|
|
215
215
|
* to be "spread" to create new schemas. Note that doing
|
|
216
|
-
* this will mean that custom and required assertions
|
|
217
|
-
* not be preserved. Compare to
|
|
218
|
-
* preserves all assertions
|
|
216
|
+
* this will mean that custom and required assertions on
|
|
217
|
+
* the object itself will not be preserved. Compare to
|
|
218
|
+
* {@link append} which preserves all assertions.
|
|
219
219
|
*/
|
|
220
220
|
export() {
|
|
221
221
|
return this.meta.fields || {};
|
|
@@ -261,15 +261,24 @@ class ObjectSchema extends _TypeSchema.default {
|
|
|
261
261
|
|
|
262
262
|
// Private
|
|
263
263
|
|
|
264
|
-
|
|
264
|
+
toJSON(extra) {
|
|
265
|
+
const {
|
|
266
|
+
stripUnknown = false
|
|
267
|
+
} = this.meta;
|
|
268
|
+
const required = [];
|
|
265
269
|
const properties = {};
|
|
266
270
|
for (let [key, schema] of Object.entries(this.export())) {
|
|
267
|
-
properties[key] = schema.
|
|
271
|
+
properties[key] = schema.toJSON(extra);
|
|
272
|
+
if (schema.meta.required) {
|
|
273
|
+
required.push(key);
|
|
274
|
+
}
|
|
268
275
|
}
|
|
269
276
|
return {
|
|
270
|
-
...super.
|
|
277
|
+
...super.toJSON(extra),
|
|
271
278
|
...(Object.keys(properties).length > 0 && {
|
|
272
|
-
properties
|
|
279
|
+
properties,
|
|
280
|
+
required,
|
|
281
|
+
additionalProperties: stripUnknown
|
|
273
282
|
})
|
|
274
283
|
};
|
|
275
284
|
}
|
package/dist/cjs/string.js
CHANGED
|
@@ -423,13 +423,13 @@ class StringSchema extends _TypeSchema.default {
|
|
|
423
423
|
|
|
424
424
|
// Private
|
|
425
425
|
|
|
426
|
-
|
|
426
|
+
toJSON(extra) {
|
|
427
427
|
const {
|
|
428
428
|
min,
|
|
429
429
|
max
|
|
430
430
|
} = this.meta;
|
|
431
431
|
return {
|
|
432
|
-
...super.
|
|
432
|
+
...super.toJSON(extra),
|
|
433
433
|
...(min && {
|
|
434
434
|
minLength: min
|
|
435
435
|
}),
|
package/dist/cjs/tuple.js
CHANGED
|
@@ -79,15 +79,15 @@ class TupleSchema extends _Schema.default {
|
|
|
79
79
|
toString() {
|
|
80
80
|
return 'tuple';
|
|
81
81
|
}
|
|
82
|
-
|
|
82
|
+
toJSON(extra) {
|
|
83
83
|
const {
|
|
84
84
|
schemas
|
|
85
85
|
} = this.meta;
|
|
86
86
|
return {
|
|
87
|
-
...super.
|
|
87
|
+
...super.toJSON(extra),
|
|
88
88
|
type: 'array',
|
|
89
89
|
prefixItems: schemas.map(schema => {
|
|
90
|
-
return schema.
|
|
90
|
+
return schema.toJSON();
|
|
91
91
|
})
|
|
92
92
|
};
|
|
93
93
|
}
|
package/package.json
CHANGED
package/src/Schema.js
CHANGED
|
@@ -206,20 +206,33 @@ export default class Schema {
|
|
|
206
206
|
return merged;
|
|
207
207
|
}
|
|
208
208
|
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
/**
|
|
210
|
+
* Exports the schema in [JSON Schema](https://json-schema.org/) format.
|
|
211
|
+
* Note that this may not represent the schema in its enitrety. Specifically,
|
|
212
|
+
* custom (code-based) assertions will not be output.
|
|
213
|
+
* @param {Object} [extra]
|
|
214
|
+
*/
|
|
215
|
+
toJSON(extra) {
|
|
216
|
+
const { format, tags } = this.meta;
|
|
211
217
|
return {
|
|
212
|
-
required,
|
|
213
218
|
format,
|
|
214
219
|
...tags,
|
|
215
220
|
...this.getAnyType(),
|
|
216
221
|
...this.getDefault(),
|
|
217
222
|
...this.getNullable(),
|
|
218
|
-
...this.
|
|
223
|
+
...this.getEnum(),
|
|
219
224
|
...this.expandExtra(extra),
|
|
220
225
|
};
|
|
221
226
|
}
|
|
222
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Exports the schema in [JSON Schema](https://json-schema.org/) format.
|
|
230
|
+
* @alias toJSON.
|
|
231
|
+
*/
|
|
232
|
+
toOpenApi(...extra) {
|
|
233
|
+
return this.toJSON(extra);
|
|
234
|
+
}
|
|
235
|
+
|
|
223
236
|
getAnyType() {
|
|
224
237
|
const { type, enum: set } = this.meta;
|
|
225
238
|
if (!type && !set) {
|
|
@@ -249,6 +262,46 @@ export default class Schema {
|
|
|
249
262
|
}
|
|
250
263
|
}
|
|
251
264
|
|
|
265
|
+
getEnum() {
|
|
266
|
+
const { enum: allowed } = this.meta;
|
|
267
|
+
if (allowed?.length) {
|
|
268
|
+
const type = typeof allowed[0];
|
|
269
|
+
const allowEnum = allowed.every((entry) => {
|
|
270
|
+
const entryType = typeof entry;
|
|
271
|
+
return entryType !== 'object' && entryType === type;
|
|
272
|
+
});
|
|
273
|
+
if (allowEnum) {
|
|
274
|
+
return {
|
|
275
|
+
type,
|
|
276
|
+
enum: allowed,
|
|
277
|
+
};
|
|
278
|
+
} else {
|
|
279
|
+
const oneOf = [];
|
|
280
|
+
for (let entry of allowed) {
|
|
281
|
+
if (isSchema(entry)) {
|
|
282
|
+
oneOf.push(entry.toJSON());
|
|
283
|
+
} else {
|
|
284
|
+
const type = typeof entry;
|
|
285
|
+
let forType = oneOf.find((el) => {
|
|
286
|
+
return el.type === type;
|
|
287
|
+
});
|
|
288
|
+
if (!forType) {
|
|
289
|
+
forType = {
|
|
290
|
+
type,
|
|
291
|
+
enum: [],
|
|
292
|
+
};
|
|
293
|
+
oneOf.push(forType);
|
|
294
|
+
}
|
|
295
|
+
if (forType.enum) {
|
|
296
|
+
forType.enum.push(entry);
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return { oneOf };
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
252
305
|
expandExtra(extra = {}) {
|
|
253
306
|
const { tag, ...rest } = extra;
|
|
254
307
|
if (typeof extra?.tag === 'function') {
|
|
@@ -258,7 +311,7 @@ export default class Schema {
|
|
|
258
311
|
}
|
|
259
312
|
|
|
260
313
|
inspect() {
|
|
261
|
-
return JSON.stringify(this.
|
|
314
|
+
return JSON.stringify(this.toJSON(), null, 2);
|
|
262
315
|
}
|
|
263
316
|
|
|
264
317
|
get() {
|
|
@@ -382,46 +435,6 @@ export default class Schema {
|
|
|
382
435
|
}
|
|
383
436
|
return value;
|
|
384
437
|
}
|
|
385
|
-
|
|
386
|
-
enumToOpenApi() {
|
|
387
|
-
const { enum: allowed } = this.meta;
|
|
388
|
-
if (allowed?.length) {
|
|
389
|
-
const type = typeof allowed[0];
|
|
390
|
-
const allowEnum = allowed.every((entry) => {
|
|
391
|
-
const entryType = typeof entry;
|
|
392
|
-
return entryType !== 'object' && entryType === type;
|
|
393
|
-
});
|
|
394
|
-
if (allowEnum) {
|
|
395
|
-
return {
|
|
396
|
-
type,
|
|
397
|
-
enum: allowed,
|
|
398
|
-
};
|
|
399
|
-
} else {
|
|
400
|
-
const oneOf = [];
|
|
401
|
-
for (let entry of allowed) {
|
|
402
|
-
if (isSchema(entry)) {
|
|
403
|
-
oneOf.push(entry.toOpenApi());
|
|
404
|
-
} else {
|
|
405
|
-
const type = typeof entry;
|
|
406
|
-
let forType = oneOf.find((el) => {
|
|
407
|
-
return el.type === type;
|
|
408
|
-
});
|
|
409
|
-
if (!forType) {
|
|
410
|
-
forType = {
|
|
411
|
-
type,
|
|
412
|
-
enum: [],
|
|
413
|
-
};
|
|
414
|
-
oneOf.push(forType);
|
|
415
|
-
}
|
|
416
|
-
if (forType.enum) {
|
|
417
|
-
forType.enum.push(entry);
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
return { oneOf };
|
|
422
|
-
}
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
438
|
}
|
|
426
439
|
|
|
427
440
|
function assertTypes(options) {
|
package/src/TypeSchema.js
CHANGED
package/src/array.js
CHANGED
|
@@ -110,23 +110,23 @@ class ArraySchema extends TypeSchema {
|
|
|
110
110
|
return 'array';
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
-
|
|
113
|
+
toJSON(extra) {
|
|
114
114
|
let other;
|
|
115
115
|
const { schemas } = this.meta;
|
|
116
116
|
if (schemas.length > 1) {
|
|
117
117
|
other = {
|
|
118
118
|
oneOf: schemas.map((schema) => {
|
|
119
|
-
return schema.
|
|
119
|
+
return schema.toJSON();
|
|
120
120
|
}),
|
|
121
121
|
};
|
|
122
122
|
} else if (schemas.length === 1) {
|
|
123
123
|
other = {
|
|
124
|
-
items: schemas[0].
|
|
124
|
+
items: schemas[0].toJSON(),
|
|
125
125
|
};
|
|
126
126
|
}
|
|
127
127
|
|
|
128
128
|
return {
|
|
129
|
-
...super.
|
|
129
|
+
...super.toJSON(extra),
|
|
130
130
|
...other,
|
|
131
131
|
type: 'array',
|
|
132
132
|
};
|
package/src/date.js
CHANGED
|
@@ -138,10 +138,10 @@ class DateSchema extends Schema {
|
|
|
138
138
|
return 'date';
|
|
139
139
|
}
|
|
140
140
|
|
|
141
|
-
|
|
141
|
+
toJSON(extra) {
|
|
142
142
|
const { format } = this.meta;
|
|
143
143
|
return {
|
|
144
|
-
...super.
|
|
144
|
+
...super.toJSON(extra),
|
|
145
145
|
type: format.includes('timestamp') ? 'number' : 'string',
|
|
146
146
|
};
|
|
147
147
|
}
|
package/src/number.js
CHANGED
|
@@ -71,10 +71,10 @@ class NumberSchema extends TypeSchema {
|
|
|
71
71
|
|
|
72
72
|
// Private
|
|
73
73
|
|
|
74
|
-
|
|
74
|
+
toJSON(extra) {
|
|
75
75
|
const { min, max, multiple } = this.meta;
|
|
76
76
|
return {
|
|
77
|
-
...super.
|
|
77
|
+
...super.toJSON(extra),
|
|
78
78
|
...(min != null && {
|
|
79
79
|
minimum: min,
|
|
80
80
|
}),
|
package/src/object.js
CHANGED
|
@@ -190,6 +190,7 @@ class ObjectSchema extends TypeSchema {
|
|
|
190
190
|
}
|
|
191
191
|
|
|
192
192
|
const update = {};
|
|
193
|
+
|
|
193
194
|
for (let field of fields) {
|
|
194
195
|
set(update, field, this.get(field).required());
|
|
195
196
|
}
|
|
@@ -200,9 +201,9 @@ class ObjectSchema extends TypeSchema {
|
|
|
200
201
|
/**
|
|
201
202
|
* Returns the schema's fields as an object allowing them
|
|
202
203
|
* to be "spread" to create new schemas. Note that doing
|
|
203
|
-
* this will mean that custom and required assertions
|
|
204
|
-
* not be preserved. Compare to
|
|
205
|
-
* preserves all assertions
|
|
204
|
+
* this will mean that custom and required assertions on
|
|
205
|
+
* the object itself will not be preserved. Compare to
|
|
206
|
+
* {@link append} which preserves all assertions.
|
|
206
207
|
*/
|
|
207
208
|
export() {
|
|
208
209
|
return this.meta.fields || {};
|
|
@@ -251,15 +252,23 @@ class ObjectSchema extends TypeSchema {
|
|
|
251
252
|
|
|
252
253
|
// Private
|
|
253
254
|
|
|
254
|
-
|
|
255
|
+
toJSON(extra) {
|
|
256
|
+
const { stripUnknown = false } = this.meta;
|
|
257
|
+
|
|
258
|
+
const required = [];
|
|
255
259
|
const properties = {};
|
|
256
260
|
for (let [key, schema] of Object.entries(this.export())) {
|
|
257
|
-
properties[key] = schema.
|
|
261
|
+
properties[key] = schema.toJSON(extra);
|
|
262
|
+
if (schema.meta.required) {
|
|
263
|
+
required.push(key);
|
|
264
|
+
}
|
|
258
265
|
}
|
|
259
266
|
return {
|
|
260
|
-
...super.
|
|
267
|
+
...super.toJSON(extra),
|
|
261
268
|
...(Object.keys(properties).length > 0 && {
|
|
262
269
|
properties,
|
|
270
|
+
required,
|
|
271
|
+
additionalProperties: stripUnknown,
|
|
263
272
|
}),
|
|
264
273
|
};
|
|
265
274
|
}
|
package/src/string.js
CHANGED
|
@@ -440,10 +440,10 @@ class StringSchema extends TypeSchema {
|
|
|
440
440
|
|
|
441
441
|
// Private
|
|
442
442
|
|
|
443
|
-
|
|
443
|
+
toJSON(extra) {
|
|
444
444
|
const { min, max } = this.meta;
|
|
445
445
|
return {
|
|
446
|
-
...super.
|
|
446
|
+
...super.toJSON(extra),
|
|
447
447
|
...(min && {
|
|
448
448
|
minLength: min,
|
|
449
449
|
}),
|
package/src/tuple.js
CHANGED
|
@@ -69,13 +69,13 @@ class TupleSchema extends Schema {
|
|
|
69
69
|
return 'tuple';
|
|
70
70
|
}
|
|
71
71
|
|
|
72
|
-
|
|
72
|
+
toJSON(extra) {
|
|
73
73
|
const { schemas } = this.meta;
|
|
74
74
|
return {
|
|
75
|
-
...super.
|
|
75
|
+
...super.toJSON(extra),
|
|
76
76
|
type: 'array',
|
|
77
77
|
prefixItems: schemas.map((schema) => {
|
|
78
|
-
return schema.
|
|
78
|
+
return schema.toJSON();
|
|
79
79
|
}),
|
|
80
80
|
};
|
|
81
81
|
}
|
package/types/Schema.d.ts
CHANGED
|
@@ -71,7 +71,18 @@ export default class Schema {
|
|
|
71
71
|
* @returns {Schema}
|
|
72
72
|
*/
|
|
73
73
|
append(schema: any): Schema;
|
|
74
|
-
|
|
74
|
+
/**
|
|
75
|
+
* Exports the schema in [JSON Schema](https://json-schema.org/) format.
|
|
76
|
+
* Note that this may not represent the schema in its enitrety. Specifically,
|
|
77
|
+
* custom (code-based) assertions will not be output.
|
|
78
|
+
* @param {Object} [extra]
|
|
79
|
+
*/
|
|
80
|
+
toJSON(extra?: any): any;
|
|
81
|
+
/**
|
|
82
|
+
* Exports the schema in [JSON Schema](https://json-schema.org/) format.
|
|
83
|
+
* @alias toJSON.
|
|
84
|
+
*/
|
|
85
|
+
toOpenApi(...extra: any[]): any;
|
|
75
86
|
getAnyType(): {
|
|
76
87
|
type: string[];
|
|
77
88
|
};
|
|
@@ -83,6 +94,7 @@ export default class Schema {
|
|
|
83
94
|
getNullable(): {
|
|
84
95
|
nullable: boolean;
|
|
85
96
|
};
|
|
97
|
+
getEnum(): any;
|
|
86
98
|
expandExtra(extra?: {}): {};
|
|
87
99
|
inspect(): string;
|
|
88
100
|
get(): void;
|
|
@@ -102,6 +114,5 @@ export default class Schema {
|
|
|
102
114
|
transform(fn: any): this;
|
|
103
115
|
getSortIndex(type: any): number;
|
|
104
116
|
runAssertion(value: any, assertion: any, options?: {}): Promise<any>;
|
|
105
|
-
enumToOpenApi(): any;
|
|
106
117
|
}
|
|
107
118
|
//# sourceMappingURL=Schema.d.ts.map
|
package/types/Schema.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAcA,kDAEC;AAED;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,uBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,YAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDAwCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,MAAM,CAMlB;IAED,
|
|
1
|
+
{"version":3,"file":"Schema.d.ts","sourceRoot":"","sources":["../src/Schema.js"],"names":[],"mappings":"AAcA,kDAEC;AAED;IACE,uBAGC;IAFC,kBAAoB;IACpB,SAAgB;IAKlB;;OAEG;IACH,YAFa,IAAI,CAQhB;IAED;;;OAGG;IACH,mBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,sBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,uBAFa,IAAI,CAShB;IAED;;;;OAIG;IACH,mBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,sBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,uBAFa,IAAI,CAIhB;IAED;;;OAGG;IACH,YAFa,IAAI,CAIhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED;;OAEG;IACH,gBAFa,IAAI,CAShB;IAED;;OAEG;IACH,+BAFa,IAAI,CAMhB;IAED;;OAEG;IACH,uBAFa,IAAI,CAIhB;IAED,iDAwCC;IAED;;OAEG;IACH,kBAFa,IAAI,CAOhB;IAED;;;OAGG;IACH,qBAFa,MAAM,CAMlB;IAED;;;;;OAKG;IACH,yBAWC;IAED;;;OAGG;IACH,gCAEC;IAED;;MAOC;IAED;;;;MASC;IAED;;MAOC;IAED,eAsCC;IAED,4BAMC;IAED,kBAEC;IAED,YAGC;IAID;;OAEG;IACH,kCAFa,IAAI,CAmDhB;IAED;;OAEG;IACH,4BAFa,IAAI,CAUhB;IAED,oCAKC;IAED,gEAQC;IAED;;OAEG;IACH,oBAFa,IAAI,CAShB;IAED,gCAGC;IAED,qEAYC;CACF"}
|
package/types/object.d.ts
CHANGED
|
@@ -55,9 +55,9 @@ declare class ObjectSchema extends TypeSchema {
|
|
|
55
55
|
/**
|
|
56
56
|
* Returns the schema's fields as an object allowing them
|
|
57
57
|
* to be "spread" to create new schemas. Note that doing
|
|
58
|
-
* this will mean that custom and required assertions
|
|
59
|
-
* not be preserved. Compare to
|
|
60
|
-
* preserves all assertions
|
|
58
|
+
* this will mean that custom and required assertions on
|
|
59
|
+
* the object itself will not be preserved. Compare to
|
|
60
|
+
* {@link append} which preserves all assertions.
|
|
61
61
|
*/
|
|
62
62
|
export(): any;
|
|
63
63
|
/**
|
package/types/object.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.js"],"names":[],"mappings":"AA4TA;;;;;;GAMG;AACH,uCAJW,SAAS,gBAQnB;wBA9TY;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,EAAE;AAD3C;;GAEG;AAEH;IACE,uBAGC;IAED,cA8EC;IAED;;;;;;OAMG;IACH,WAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAsB9B;IAED;;;;;;OAMG;IACH,cAFW,MAAM,GAAC,KAAK,CAAC,MAAM,CAAC,OAY9B;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;OAIG;IACH,gBAFc,MAAM,EAAA,gBASnB;IAED;;;;;;;OAOG;IACH,mBAHc,MAAM,EAAA,gBAmBnB;IAED;;;;;;OAMG;IACH,cAEC;IAED;;;;;;;;;OASG;IACH,YAFW,SAAS,GAAC,MAAM,gBA+B1B;CAwBF;mBA9QgC,UAAU;uBAFpB,cAAc"}
|