You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+24Lines changed: 24 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -274,7 +274,31 @@ Output:
274
274
275
275
{valid: true}
276
276
277
+
## Extend schema
278
+
279
+
Normally inheritance with JSON Schema is achieved with `allOf`. However when `.additionalProperties(false)` is used the validator won't
280
+
understand which properties come from the base schema. `S.extend` creates a schema merging the base into the new one so
281
+
that the validator knows all the properties because it's evaluating only a single schema.
282
+
For example in a CRUD API `POST /users` could use the `userBaseSchema` rather than `GET /users` or `PATCH /users` use the `userSchema`
283
+
which contains the `id`, `createdAt` and `updatedAt` generated server side.
284
+
285
+
```js
286
+
constS=require('fluent-schema')
287
+
constuserBaseSchema=S.object()
288
+
.additionalProperties(false)
289
+
.prop('username', S.string())
290
+
.prop('password', S.string())
291
+
292
+
constuserSchema=S.extend(userBaseSchema)
293
+
.prop('id', S.string().format('uuid'))
294
+
.prop('createdAt', S.string().format('time'))
295
+
.prop('updatedAt', S.string().format('time'))
296
+
297
+
console.log(userSchema)
298
+
```
299
+
277
300
### Detect Fluent Schema objects
301
+
278
302
Every Fluent Schema objects contains a boolean `isFluentSchema`. In this way you can write your own utilities that understands the Fluent Schema API and improve the user experience of your tool.
0 commit comments