Skip to content

Commit d3e5623

Browse files
committed
Move to S syntax first step
1 parent e8db6ae commit d3e5623

23 files changed

+616
-1024
lines changed

README.md

Lines changed: 41 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ A fluent API to generate JSON schemas (draft-07) for Node.js and browser.
1212
- Faster and shorter way to write a JSON Schema via a [fluent API](https://en.wikipedia.org/wiki/Fluent_interface)
1313
- Runtime errors for invalid options or keywords misuse
1414
- Javascript constants can be used in the JSON schema (e.g. _enum_, _const_, _default_ ) avoiding discrepancies between model and schema
15-
- Typescript friendly
16-
- No dependencies
15+
- Typescript definitions
16+
- Zero dependencies
1717

1818
## Install
1919

@@ -26,35 +26,27 @@ or
2626
## Usage
2727

2828
```javascript
29-
const userSchema = FluentSchema()
30-
.object()
29+
const S = require('fluent-schema')
30+
const schema = S.object()
3131
.id('http://foo/user')
3232
.title('My First Fluent JSON Schema')
3333
.description('A simple user')
3434
.prop(
3535
'email',
36-
FluentSchema()
37-
.string()
38-
.format(FORMATS.EMAIL)
36+
S.string()
37+
.format(S.FORMATS.EMAIL)
3938
.required()
4039
)
4140
.prop(
4241
'password',
43-
FluentSchema()
44-
.string()
42+
S.string()
4543
.minLength(8)
4644
.required()
4745
)
48-
.prop(
49-
'role',
50-
FluentSchema()
51-
.enum(['ADMIN', 'USER'])
52-
.default('USER')
53-
)
46+
.prop('role', S.enum(['ADMIN', 'USER']).default('USER'))
5447
.definition(
5548
'address',
56-
FluentSchema()
57-
.object()
49+
S.object()
5850
.id('#address')
5951
.prop('line1')
6052
.required()
@@ -69,7 +61,7 @@ const userSchema = FluentSchema()
6961
.prop('address')
7062
.ref('#address')
7163

72-
console.log(JSON.stringify(userSchema.valueOf(), undefined, 2))
64+
console.log(JSON.stringify(schema.valueOf(), undefined, 2))
7365
```
7466

7567
Schema generated:
@@ -81,7 +73,6 @@ Schema generated:
8173
"address": {
8274
"type": "object",
8375
"$id": "#address",
84-
"required": ["line1", "country", "city", "zipcode"],
8576
"properties": {
8677
"line1": {
8778
"type": "string"
@@ -98,7 +89,8 @@ Schema generated:
9889
"zipcode": {
9990
"type": "string"
10091
}
101-
}
92+
},
93+
"required": ["line1", "country", "city", "zipcode"]
10294
}
10395
},
10496
"type": "object",
@@ -115,9 +107,9 @@ Schema generated:
115107
"minLength": 8
116108
},
117109
"role": {
118-
"type": "object",
110+
"enum": ["ADMIN", "USER"],
119111
"default": "USER",
120-
"enum": ["ADMIN", "USER"]
112+
"type": "string"
121113
},
122114
"address": {
123115
"$ref": "#address"
@@ -127,6 +119,30 @@ Schema generated:
127119
}
128120
```
129121

122+
## Typescript
123+
124+
with `"esModuleInterop": true` activated in the `tsconfig.json`
125+
126+
```typescript
127+
import S from 'fluent-schema'
128+
129+
const schema = S.object()
130+
.prop('foo', S.string())
131+
.prop('bar', S.number())
132+
.valueOf()
133+
```
134+
135+
with `"esModuleInterop": false` in the `tsconfig.json`
136+
137+
```typescript
138+
import * as S from 'fluent-schema'
139+
140+
const schema = S.object()
141+
.prop('foo', S.string())
142+
.prop('bar', S.number())
143+
.valueOf()
144+
```
145+
130146
## Validation
131147

132148
Fluent schema **doesn't** validate a JSON schema. However there are many libraries that can do that for you.
@@ -144,7 +160,7 @@ Snippet
144160

145161
```javascript
146162
const ajv = new Ajv({ allErrors: true })
147-
const validate = ajv.compile(userSchema.valueOf())
163+
const validate = ajv.compile(schema.valueOf())
148164
let user = {}
149165
let valid = validate(user)
150166
console.log({ valid }) //=> {valid: false}
@@ -242,76 +258,12 @@ console.log({ valid })
242258

243259
Output:
244260

245-
{valid: true}
246-
247-
## Validation Keywords Supported
248-
249-
[Reference](https://json-schema.org/latest/json-schema-validation.html):
250-
251-
1. Validation Keywords for Any Instance Type
252-
253-
- [x] types
254-
- [x] string
255-
- [x] boolean
256-
- [x] number
257-
- [x] integer
258-
- [x] array
259-
- [x] object
260-
- [x] null
261-
- [x] enum
262-
- [x] const
263-
264-
2. Validation Keywords for Numeric Instances (number and integer)
265-
266-
- [x] multipleOf
267-
- [x] maximum
268-
- [x] exclusiveMaximum
269-
- [x] minimum
270-
- [x] exclusiveMinimum
271-
272-
3. Validation Keywords for Strings
273-
274-
- [x] maxLength
275-
- [x] minLength
276-
- [x] pattern
277-
- [x] format
278-
279-
4. Validation Keywords for Arrays
280-
281-
- [x] items
282-
- [x] additionalItems
283-
- [x] maxItems
284-
- [x] minItems
285-
- [x] uniqueItems
286-
- [x] contains
287-
288-
5. Validation Keywords for Objects
289-
290-
- [x] maxProperties
291-
- [x] minProperties
292-
- [x] required
293-
- [x] properties
294-
- [x] patternProperties
295-
- [x] additionalProperties
296-
- [x] dependencies
297-
- [x] propertyNames
298-
299-
6. Keywords for Applying Subschemas Conditionally
300-
301-
- [x] if
302-
- [x] then
303-
- [x] else
304-
305-
7. Keywords for Applying Subschemas With Boolean Logic
306-
307-
- [x] allOf
308-
- [x] anyOf
309-
- [x] oneOf
310-
- [x] not
261+
{valid: true}
311262

312263
## Documentation
313264

314265
[API Doc](./docs/API.md).
266+
[JSON schema reference](https://json-schema.org/latest/json-schema-validation.html):
315267

316268
## Acknowledgments
317269

0 commit comments

Comments
 (0)