Skip to content

Commit

Permalink
add readOnly and writeOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
rrufus authored and aboutlo committed May 28, 2019
1 parent c5b9564 commit 5a8a005
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fluent-schema",
"version": "0.7.1",
"version": "0.7.2",
"description": "JSON Schema fluent API",
"main": "src/FluentSchema.js",
"typings": "src/FluentSchema.d.ts",
Expand Down
28 changes: 28 additions & 0 deletions src/BaseSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,34 @@ const BaseSchema = (
return setAttribute({ schema, ...options }, ['default', defaults, 'any'])
},

/**
* The value of readOnly can be left empty to indicate the property is readOnly.
* It takes an optional boolean which can be used to explicitly set readOnly true/false
*
* {@link readOnly|https://json-schema.org/latest/json-schema-validation.html#rfc.section.10.3}
* @param {boolean|undefined} isReadOnly
* @returns {BaseSchema}
*/

readOnly: isReadOnly => {
const value = isReadOnly !== undefined ? isReadOnly : true
return setAttribute({ schema, ...options }, ['readOnly', value, 'boolean'])
},

/**
* The value of writeOnly can be left empty to indicate the property is writeOnly.
* It takes an optional boolean which can be used to explicitly set writeOnly true/false
*
* {@link writeOnly|https://json-schema.org/latest/json-schema-validation.html#rfc.section.10.3}
* @param {boolean|undefined} isWriteOnly
* @returns {BaseSchema}
*/

writeOnly: isWriteOnly => {
const value = isWriteOnly !== undefined ? isWriteOnly : true
return setAttribute({ schema, ...options }, ['writeOnly', value, 'boolean'])
},

/**
* Required has to be chained to a property:
* Examples:
Expand Down
48 changes: 48 additions & 0 deletions src/BaseSchema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,54 @@ describe('BaseSchema', () => {
})
})

describe('readOnly', () => {
it('valid', () => {
expect(
BaseSchema()
.readOnly(true)
.valueOf().readOnly
).toEqual(true)
})
it('valid with no value', () => {
expect(
BaseSchema()
.readOnly()
.valueOf().readOnly
).toEqual(true)
})
it('can be set to false', () => {
expect(
BaseSchema()
.readOnly(false)
.valueOf().readOnly
).toEqual(false)
})
})

describe('writeOnly', () => {
it('valid', () => {
expect(
BaseSchema()
.writeOnly(true)
.valueOf().writeOnly
).toEqual(true)
})
it('valid with no value', () => {
expect(
BaseSchema()
.writeOnly()
.valueOf().writeOnly
).toEqual(true)
})
it('can be set to false', () => {
expect(
BaseSchema()
.writeOnly(false)
.valueOf().writeOnly
).toEqual(false)
})
})

describe('ref', () => {
it('base', () => {
const ref = 'myRef'
Expand Down
2 changes: 2 additions & 0 deletions src/FluentSchema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export interface BaseSchema<T> {
anyOf: (schema: Array<JSONSchema>) => T
allOf: (schema: Array<JSONSchema>) => T
oneOf: (schema: Array<JSONSchema>) => T
readOnly: (isReadOnly?: boolean) => T
writeOnly: (isWriteOnly?: boolean) => T
}

export type TYPE =
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const schema = S.object()
.required()
.prop('age', S.mixed<NumberSchema & StringSchema>(['string', 'integer']))
.ifThen(S.object().prop('age', S.string()), S.required(['age']))
.readOnly()
.writeOnly(true)

.valueOf()

Expand Down

0 comments on commit 5a8a005

Please sign in to comment.