diff --git a/package.json b/package.json index e84b9c0..f040751 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/BaseSchema.js b/src/BaseSchema.js index da509db..01ac9c4 100644 --- a/src/BaseSchema.js +++ b/src/BaseSchema.js @@ -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: diff --git a/src/BaseSchema.test.js b/src/BaseSchema.test.js index ce8b158..3cfd13e 100644 --- a/src/BaseSchema.test.js +++ b/src/BaseSchema.test.js @@ -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' diff --git a/src/FluentSchema.d.ts b/src/FluentSchema.d.ts index 3e41571..1db3ff0 100644 --- a/src/FluentSchema.d.ts +++ b/src/FluentSchema.d.ts @@ -18,6 +18,8 @@ export interface BaseSchema { anyOf: (schema: Array) => T allOf: (schema: Array) => T oneOf: (schema: Array) => T + readOnly: (isReadOnly?: boolean) => T + writeOnly: (isWriteOnly?: boolean) => T } export type TYPE = diff --git a/src/types/index.ts b/src/types/index.ts index c1f96b1..3b21499 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -48,6 +48,8 @@ const schema = S.object() .required() .prop('age', S.mixed(['string', 'integer'])) .ifThen(S.object().prop('age', S.string()), S.required(['age'])) + .readOnly() + .writeOnly(true) .valueOf()