Skip to content

Commit 69dca5d

Browse files
committed
if prop type isn't defined it means any not string
1 parent bc74a98 commit 69dca5d

File tree

8 files changed

+92
-97
lines changed

8 files changed

+92
-97
lines changed

README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ A fluent API to generate JSON schemas (draft-07) for Node.js and browser.
44

55
[![view on npm](https://img.shields.io/npm/v/fluent-schema.svg)](https://www.npmjs.org/package/fluent-schema)
66
[![Build Status](https://travis-ci.com/fastify/fluent-schema.svg?branch=master)](https://travis-ci.com/fastify/fluent-schema?branch=master)
7-
[![Coverage Status](https://coveralls.io/repos/github/fastify/fluent-schema/badge.svg?branch=master)](https://coveralls.io/github/fastify/fluent-schema?branch=master)
87

98
## Features
109

@@ -14,6 +13,7 @@ A fluent API to generate JSON schemas (draft-07) for Node.js and browser.
1413
- Javascript constants can be used in the JSON schema (e.g. _enum_, _const_, _default_ ) avoiding discrepancies between model and schema
1514
- Typescript definitions
1615
- Zero dependencies
16+
- Coverage 99%
1717

1818
## Install
1919

@@ -27,6 +27,12 @@ or
2727

2828
```javascript
2929
const S = require('fluent-schema')
30+
31+
const ROLES = {
32+
ADMIN: 'ADMIN',
33+
USER: 'USER',
34+
}
35+
3036
const schema = S.object()
3137
.id('http://foo/user')
3238
.title('My First Fluent JSON Schema')
@@ -43,23 +49,24 @@ const schema = S.object()
4349
.minLength(8)
4450
.required()
4551
)
46-
.prop('role', S.enum(['ADMIN', 'USER']).default('USER'))
52+
.prop(
53+
'role',
54+
S.string()
55+
.enum(Object.values(ROLES))
56+
.default(ROLES.USER)
57+
)
4758
.definition(
4859
'address',
4960
S.object()
5061
.id('#address')
51-
.prop('line1')
52-
.required()
53-
.prop('line2')
54-
.prop('country')
55-
.required()
56-
.prop('city')
57-
.required()
58-
.prop('zipcode')
59-
.required()
62+
.prop('line1', S.string())
63+
.prop('line2', S.string())
64+
.prop('country', S.string())
65+
.prop('city', S.string())
66+
.prop('zipcode', S.string())
67+
.required(['line1', 'country', 'city', 'zipcode'])
6068
)
61-
.prop('address')
62-
.ref('#address')
69+
.prop('address', S.ref('#address'))
6370

6471
console.log(JSON.stringify(schema.valueOf(), undefined, 2))
6572
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"devDependencies": {
5757
"ajv": "^6.5.5",
5858
"husky": "^1.1.3",
59-
"jest": "^23.6.0",
59+
"jest": "^24.3.1",
6060
"jsdoc-to-markdown": "^4.0.1",
6161
"lint-staged": "^8.0.4",
6262
"prettier": "^1.14.3",

src/BaseSchema.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ describe('BaseSchema', () => {
146146
it('nested', () => {
147147
expect(
148148
S.object()
149-
.prop('foo')
150-
.prop('bar', S.required())
149+
.prop('foo', S.string())
150+
.prop('bar', S.string().required())
151151
.required(['foo'])
152152
.valueOf()
153153
).toEqual({

src/FluentSchema.integration.test.js

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ describe('S', () => {
1616
describe('basic', () => {
1717
const ajv = new Ajv()
1818
const schema = S.object()
19-
.prop('username')
20-
.prop('password')
19+
.prop('username', S.string())
20+
.prop('password', S.string())
2121
.valueOf()
2222
const validate = ajv.compile(schema)
2323

@@ -54,7 +54,7 @@ describe('S', () => {
5454
.ifThen(
5555
S.object().prop('prop', S.string().maxLength(5)),
5656
S.object()
57-
.prop('extraProp')
57+
.prop('extraProp', S.string())
5858
.required()
5959
)
6060
.valueOf()
@@ -94,10 +94,10 @@ describe('S', () => {
9494
.ifThenElse(
9595
S.object().prop('ifProp', S.string().enum([VALUES[0]])),
9696
S.object()
97-
.prop('thenProp')
97+
.prop('thenProp', S.string())
9898
.required(),
9999
S.object()
100-
.prop('elseProp')
100+
.prop('elseProp', S.string())
101101
.required()
102102
)
103103
.valueOf()
@@ -136,17 +136,16 @@ describe('S', () => {
136136
'address',
137137
S.object()
138138
.id('#/definitions/address')
139-
.prop('street_address')
139+
.prop('street_address', S.string())
140140
.required()
141-
.prop('city')
142-
.required()
143-
.prop('state')
141+
.prop('city', S.string())
144142
.required()
143+
.prop('state', S.string().required())
145144
)
146145
.allOf([
147146
S.ref('#/definitions/address'),
148147
S.object()
149-
.prop('type')
148+
.prop('type', S.string())
150149
.enum(['residential', 'business']),
151150
])
152151
.valueOf()
@@ -235,18 +234,18 @@ describe('S', () => {
235234
.default(false)
236235
.required()
237236
)
238-
.prop('thenFooA')
239-
.prop('thenFooB')
237+
.prop('thenFooA', S.string())
238+
.prop('thenFooB', S.string())
240239
.allOf([
241240
S.ifThen(
242241
S.object()
243-
.prop('foo')
242+
.prop('foo', S.string())
244243
.enum(['foo']),
245244
S.required(['thenFooA', 'thenFooB'])
246245
),
247246
S.ifThen(
248247
S.object()
249-
.prop('bar')
248+
.prop('bar', S.string())
250249
.enum(['BAR']),
251250
S.required(['thenBarA', 'thenBarB'])
252251
),
@@ -310,11 +309,11 @@ describe('S', () => {
310309
'address',
311310
S.object()
312311
.id('#address')
313-
.prop('country')
314-
.prop('city')
315-
.prop('zipcode')
312+
.prop('country', S.string())
313+
.prop('city', S.string())
314+
.prop('zipcode', S.string())
316315
)
317-
.prop('username')
316+
.prop('username', S.string())
318317
.required()
319318
.prop('password', S.string().required())
320319
.prop('address', S.object().ref('#address'))
@@ -325,7 +324,7 @@ describe('S', () => {
325324
S.object()
326325
.id('http://foo.com/role')
327326
.required()
328-
.prop('name')
327+
.prop('name', S.string())
329328
.prop('permissions')
330329
)
331330
.prop('age', S.number())
@@ -416,7 +415,7 @@ describe('S', () => {
416415
.description('The unique identifier for a product')
417416
.required()
418417
)
419-
.prop('name')
418+
.prop('name', S.string())
420419
.required()
421420
.prop(
422421
'price',

src/FluentSchema.test.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('S', () => {
1919
expect(
2020
S.withOptions({ generateIds: true })
2121
.object()
22-
.prop('prop')
22+
.prop('prop', S.string())
2323
.valueOf()
2424
).toEqual({
2525
$schema: 'http://json-schema.org/draft-07/schema#',
@@ -31,7 +31,7 @@ describe('S', () => {
3131
it('false', () => {
3232
expect(
3333
S.object()
34-
.prop('prop')
34+
.prop('prop', S.string())
3535
.valueOf()
3636
).toEqual({
3737
$schema: 'http://json-schema.org/draft-07/schema#',
@@ -48,7 +48,7 @@ describe('S', () => {
4848
.prop(
4949
'foo',
5050
S.object()
51-
.prop('bar')
51+
.prop('bar', S.string())
5252
.required()
5353
)
5454
.valueOf()
@@ -107,8 +107,8 @@ describe('S', () => {
107107
.definition(
108108
'entity',
109109
S.object()
110-
.prop('foo')
111-
.prop('bar')
110+
.prop('foo', S.string())
111+
.prop('bar', S.string())
112112
)
113113
.prop('prop')
114114
.ref('entity')
@@ -146,7 +146,7 @@ describe('S', () => {
146146
'entity',
147147
S.object()
148148
.id('myCustomId')
149-
.prop('foo')
149+
.prop('foo', S.string())
150150
)
151151
.prop('prop')
152152
.ref('entity')
@@ -254,13 +254,13 @@ describe('S', () => {
254254
'address',
255255
S.object()
256256
.id('#address')
257-
.prop('country')
258-
.prop('city')
259-
.prop('zipcode')
257+
.prop('country', S.string())
258+
.prop('city', S.string())
259+
.prop('zipcode', S.string())
260260
)
261-
.prop('username')
261+
.prop('username', S.string())
262262
.required()
263-
.prop('password')
263+
.prop('password', S.string())
264264
.required()
265265
.prop('address', S.ref('#address'))
266266

@@ -269,8 +269,8 @@ describe('S', () => {
269269
'role',
270270
S.object()
271271
.id('http://foo.com/role')
272-
.prop('name')
273-
.prop('permissions')
272+
.prop('name', S.string())
273+
.prop('permissions', S.string())
274274
)
275275
.required()
276276
.prop('age', S.number())

src/ObjectSchema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
215215

216216
const type = hasCombiningKeywords(attributes)
217217
? undefined
218-
: attributes.type || 'string'
218+
: attributes.type
219219

220220
const $ref = attributes.$ref
221221

0 commit comments

Comments
 (0)