Skip to content

Commit

Permalink
Object schema without (#160)
Browse files Browse the repository at this point in the history
* creating without method on ObjectSchema

* proper documentation

* rollback wrong push
  • Loading branch information
victortosts authored Jan 10, 2022
1 parent 96e606c commit 88f72dd
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 4 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,11 +295,13 @@ const userSchema = S.object()
console.log(userSchema)
```

## Selecting only certain properties of your schema
## Selecting certain properties of your schema

In addition to extending schemas, it is also possible to reduce them into smaller schemas. This comes in handy
when you have a large Fluent Schema, and would like to re-use some of its properties.

Select only properties you want to keep.

```js
const S = require('fluent-json-schema')
const userSchema = S.object()
Expand All @@ -312,6 +314,20 @@ const userSchema = S.object()
const loginSchema = userSchema.only(['username', 'password'])
```

Or remove properties you dont want to keep.

```js
const S = require('fluent-json-schema')
const personSchema = S.object()
.prop('name', S.string())
.prop('age', S.number())
.prop('id', S.string().format('uuid'))
.prop('createdAt', S.string().format('time'))
.prop('updatedAt', S.string().format('time'))

const bodySchema = personSchema.without(['createdAt', 'updatedAt'])
```

### Detect Fluent Schema objects

Every Fluent Schema object 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.
Expand Down
21 changes: 18 additions & 3 deletions src/ObjectSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,15 +344,30 @@ const ObjectSchema = ({ schema = initialState, ...options } = {}) => {
return ObjectSchema({
schema: {
...schema,
properties: schema.properties.filter(p =>
properties.includes(p.name)
),
properties: schema.properties.filter(({ name }) => properties.includes(name)),
required: schema.required.filter(p => properties.includes(p)),
},
...options,
})
},

/**
* Returns an object schema without a subset of keys provided
*
* @param properties a list of properties you dont want to keep
* @returns {ObjectSchema}
*/
without: properties => {
return ObjectSchema({
schema: {
...schema,
properties: schema.properties.filter(p => !properties.includes(p.name)),
required: schema.required.filter(p => !properties.includes(p)),
},
...options,
})
},

/**
* The "definitions" keywords provides a standardized location for schema authors to inline re-usable JSON Schemas into a more general schema.
* There are no restrictions placed on the values within the array.
Expand Down
62 changes: 62 additions & 0 deletions src/ObjectSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -945,6 +945,68 @@ describe('ObjectSchema', () => {
})
})

describe('without', () => {
it('returns a subset of the object', () => {
const base = S.object()
.id('base')
.title('base')
.prop('foo', S.string())
.prop('bar', S.string())
.prop('baz', S.string())
.prop(
'children',
S.object()
.prop('alpha', S.string())
.prop('beta', S.string())
)

const without = base.without(['foo', 'children'])

expect(without.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'base',
title: 'base',
properties: {
bar: {
type: 'string',
},
baz: {
type: 'string',
},
},
type: 'object',
})
})

it('works correctly with required properties', () => {
const base = S.object()
.id('base')
.title('base')
.prop('foo', S.string().required())
.prop('bar', S.string())
.prop('baz', S.string().required())
.prop('qux', S.string())

const without = base.without(['foo', 'bar'])

expect(without.valueOf()).toEqual({
$schema: 'http://json-schema.org/draft-07/schema#',
$id: 'base',
title: 'base',
properties: {
baz: {
type: 'string',
},
qux: {
type: 'string',
},
},
required: ['baz'],
type: 'object',
})
})
})

describe('raw', () => {
it('allows to add a custom attribute', () => {
const schema = ObjectSchema()
Expand Down

0 comments on commit 88f72dd

Please sign in to comment.