Skip to content

Commit de9cde6

Browse files
committed
Fix null TS definition and null detection in isFluentSchema
1 parent 6e3d353 commit de9cde6

File tree

4 files changed

+40
-4
lines changed

4 files changed

+40
-4
lines changed

src/BaseSchema.test.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ describe('BaseSchema', () => {
206206
})
207207

208208
describe('ref', () => {
209-
it('ref', () => {
209+
it('base', () => {
210210
const ref = 'myRef'
211211
expect(
212212
BaseSchema()
@@ -215,7 +215,7 @@ describe('BaseSchema', () => {
215215
).toEqual({ $ref: ref })
216216
})
217217

218-
it('ref', () => {
218+
it('S', () => {
219219
const ref = 'myRef'
220220
expect(S.ref(ref).valueOf()).toEqual({
221221
$ref: ref,
@@ -226,7 +226,7 @@ describe('BaseSchema', () => {
226226

227227
describe('combining keywords:', () => {
228228
describe('allOf', () => {
229-
it('valid', () => {
229+
it('base', () => {
230230
expect(
231231
BaseSchema()
232232
.allOf([BaseSchema().id('foo')])
@@ -235,6 +235,11 @@ describe('BaseSchema', () => {
235235
allOf: [{ $id: 'foo' }],
236236
})
237237
})
238+
it('S', () => {
239+
expect(S.allOf([S.id('foo')]).valueOf()).toEqual({
240+
allOf: [{ $id: 'foo' }],
241+
})
242+
})
238243
describe('invalid', () => {
239244
it('not an array', () => {
240245
expect(() => {
@@ -263,6 +268,33 @@ describe('BaseSchema', () => {
263268
anyOf: [{ $id: 'foo' }],
264269
})
265270
})
271+
it('S nested', () => {
272+
expect(
273+
S.object()
274+
.prop('prop', S.anyOf([S.string(), S.null()]))
275+
.valueOf()
276+
).toEqual({
277+
$schema: 'http://json-schema.org/draft-07/schema#',
278+
properties: {
279+
prop: { anyOf: [{ type: 'string' }, { type: 'null' }] },
280+
},
281+
type: 'object',
282+
})
283+
})
284+
285+
it('S nested', () => {
286+
expect(
287+
S.object()
288+
.prop('prop', S.anyOf([]).required())
289+
.valueOf()
290+
).toEqual({
291+
$schema: 'http://json-schema.org/draft-07/schema#',
292+
properties: { prop: {} },
293+
required: ['prop'],
294+
type: 'object',
295+
})
296+
})
297+
266298
describe('invalid', () => {
267299
it('not an array', () => {
268300
expect(() => {

src/FluentSchema.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export type JSONSchema =
4747
| ArraySchema
4848
| IntegerSchema
4949
| BooleanSchema
50+
| NullSchema
5051

5152
export interface SchemaOptions {
5253
schema: object

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const schema = S.object()
1010
'address',
1111
S.object()
1212
.id('#address')
13+
.prop('line1')
14+
.prop('line2', S.anyOf([S.string(), S.null()]))
1315
.prop('country')
1416
.allOf([S.string()])
1517
.prop('city')

src/utils.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ const isFluentSchema = obj =>
55
typeof obj.definition === 'function' || // ObjectSchema
66
typeof obj.items === 'function' || // ArraySchema
77
typeof obj.min === 'function' || // NumberSchema & IntegerSchema
8-
typeof obj.format === 'function') // StringSchema
8+
typeof obj.format === 'function' || // StringSchema
9+
typeof obj.null === 'function') // NullSchema
910

1011
const hasCombiningKeywords = attributes =>
1112
attributes.allOf || attributes.anyOf || attributes.oneOf || attributes.not

0 commit comments

Comments
 (0)