Skip to content

Commit 5a8a005

Browse files
rrufusaboutlo
authored andcommitted
add readOnly and writeOnly
1 parent c5b9564 commit 5a8a005

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fluent-schema",
3-
"version": "0.7.1",
3+
"version": "0.7.2",
44
"description": "JSON Schema fluent API",
55
"main": "src/FluentSchema.js",
66
"typings": "src/FluentSchema.d.ts",

src/BaseSchema.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,34 @@ const BaseSchema = (
140140
return setAttribute({ schema, ...options }, ['default', defaults, 'any'])
141141
},
142142

143+
/**
144+
* The value of readOnly can be left empty to indicate the property is readOnly.
145+
* It takes an optional boolean which can be used to explicitly set readOnly true/false
146+
*
147+
* {@link readOnly|https://json-schema.org/latest/json-schema-validation.html#rfc.section.10.3}
148+
* @param {boolean|undefined} isReadOnly
149+
* @returns {BaseSchema}
150+
*/
151+
152+
readOnly: isReadOnly => {
153+
const value = isReadOnly !== undefined ? isReadOnly : true
154+
return setAttribute({ schema, ...options }, ['readOnly', value, 'boolean'])
155+
},
156+
157+
/**
158+
* The value of writeOnly can be left empty to indicate the property is writeOnly.
159+
* It takes an optional boolean which can be used to explicitly set writeOnly true/false
160+
*
161+
* {@link writeOnly|https://json-schema.org/latest/json-schema-validation.html#rfc.section.10.3}
162+
* @param {boolean|undefined} isWriteOnly
163+
* @returns {BaseSchema}
164+
*/
165+
166+
writeOnly: isWriteOnly => {
167+
const value = isWriteOnly !== undefined ? isWriteOnly : true
168+
return setAttribute({ schema, ...options }, ['writeOnly', value, 'boolean'])
169+
},
170+
143171
/**
144172
* Required has to be chained to a property:
145173
* Examples:

src/BaseSchema.test.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,54 @@ describe('BaseSchema', () => {
205205
})
206206
})
207207

208+
describe('readOnly', () => {
209+
it('valid', () => {
210+
expect(
211+
BaseSchema()
212+
.readOnly(true)
213+
.valueOf().readOnly
214+
).toEqual(true)
215+
})
216+
it('valid with no value', () => {
217+
expect(
218+
BaseSchema()
219+
.readOnly()
220+
.valueOf().readOnly
221+
).toEqual(true)
222+
})
223+
it('can be set to false', () => {
224+
expect(
225+
BaseSchema()
226+
.readOnly(false)
227+
.valueOf().readOnly
228+
).toEqual(false)
229+
})
230+
})
231+
232+
describe('writeOnly', () => {
233+
it('valid', () => {
234+
expect(
235+
BaseSchema()
236+
.writeOnly(true)
237+
.valueOf().writeOnly
238+
).toEqual(true)
239+
})
240+
it('valid with no value', () => {
241+
expect(
242+
BaseSchema()
243+
.writeOnly()
244+
.valueOf().writeOnly
245+
).toEqual(true)
246+
})
247+
it('can be set to false', () => {
248+
expect(
249+
BaseSchema()
250+
.writeOnly(false)
251+
.valueOf().writeOnly
252+
).toEqual(false)
253+
})
254+
})
255+
208256
describe('ref', () => {
209257
it('base', () => {
210258
const ref = 'myRef'

src/FluentSchema.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ export interface BaseSchema<T> {
1818
anyOf: (schema: Array<JSONSchema>) => T
1919
allOf: (schema: Array<JSONSchema>) => T
2020
oneOf: (schema: Array<JSONSchema>) => T
21+
readOnly: (isReadOnly?: boolean) => T
22+
writeOnly: (isWriteOnly?: boolean) => T
2123
}
2224

2325
export type TYPE =

src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ const schema = S.object()
4848
.required()
4949
.prop('age', S.mixed<NumberSchema & StringSchema>(['string', 'integer']))
5050
.ifThen(S.object().prop('age', S.string()), S.required(['age']))
51+
.readOnly()
52+
.writeOnly(true)
5153

5254
.valueOf()
5355

0 commit comments

Comments
 (0)