Skip to content

Commit 5382d9b

Browse files
delvedoraboutlo
authored andcommitted
Added symbol to identify fluent schema objects (#38)
* Added symbol to identify fluent schema objects * Updated README.md * Updated isFluentSchema utility
1 parent 5a8a005 commit 5382d9b

14 files changed

+74
-11
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,18 @@ Output:
267267

268268
{valid: true}
269269

270+
### Detect Fluent Schema objects
271+
Every Fluent Schema objects contains a symbol that you can access with `Symbol.for('fluent-schema-object')`. In this way you can write your own utilities that understands the Fluent Schema API and improve the user experience of your tool.
272+
273+
```js
274+
const S = require('fluent-schema')
275+
276+
const schema = S.object()
277+
.prop('foo', S.string())
278+
.prop('bar', S.number())
279+
console.log(schema[Symbol.for('fluent-schema-object')]) // true
280+
```
281+
270282
## Documentation
271283

272284
[API Doc](./docs/API.md).

src/ArraySchema.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ describe('ArraySchema', () => {
66
expect(ArraySchema).toBeDefined()
77
})
88

9+
it('Expose symbol', () => {
10+
expect(ArraySchema()[Symbol.for('fluent-schema-object')]).toBeDefined()
11+
})
12+
913
describe('constructor', () => {
1014
it('without params', () => {
1115
expect(ArraySchema().valueOf()).toEqual({

src/BaseSchema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88
REQUIRED,
99
setAttribute,
1010
setComposeType,
11+
FLUENT_SCHEMA,
1112
} = require('./utils')
1213

1314
const initialState = {
@@ -29,6 +30,7 @@ const BaseSchema = (
2930
factory: BaseSchema,
3031
}
3132
) => ({
33+
[FLUENT_SCHEMA]: true,
3234
/**
3335
* It defines a URI for the schema, and the base URI that other URI references within the schema are resolved against.
3436
*

src/BaseSchema.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ describe('BaseSchema', () => {
66
expect(BaseSchema).toBeDefined()
77
})
88

9+
it('Expose symbol', () => {
10+
expect(BaseSchema()[Symbol.for('fluent-schema-object')]).toBeDefined()
11+
})
12+
913
describe('factory', () => {
1014
it('without params', () => {
1115
expect(BaseSchema().valueOf()).toEqual({})

src/BooleanSchema.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ describe('BooleanSchema', () => {
66
expect(BooleanSchema).toBeDefined()
77
})
88

9+
it('Expose symbol', () => {
10+
expect(BooleanSchema()[Symbol.for('fluent-schema-object')]).toBeDefined()
11+
})
12+
913
describe('constructor', () => {
1014
it('without params', () => {
1115
expect(BooleanSchema().valueOf()).toEqual({

src/IntegerSchema.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ describe('IntegerSchema', () => {
66
expect(IntegerSchema).toBeDefined()
77
})
88

9+
it('Expose symbol', () => {
10+
expect(IntegerSchema()[Symbol.for('fluent-schema-object')]).toBeDefined()
11+
})
12+
913
describe('constructor', () => {
1014
it('without params', () => {
1115
expect(IntegerSchema().valueOf()).toEqual({

src/MixedSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const { IntegerSchema } = require('./IntegerSchema')
88
const { ObjectSchema } = require('./ObjectSchema')
99
const { ArraySchema } = require('./ArraySchema')
1010

11-
const { TYPES, setAttribute } = require('./utils')
11+
const { TYPES, setAttribute, FLUENT_SCHEMA } = require('./utils')
1212

1313
const initialState = {
1414
type: [],
@@ -32,6 +32,7 @@ const MixedSchema = ({ schema = initialState, ...options } = {}) => {
3232
...options,
3333
}
3434
return {
35+
[FLUENT_SCHEMA]: true,
3536
...(schema.type.includes(TYPES.STRING)
3637
? StringSchema({ ...options, schema, factory: MixedSchema })
3738
: {}),

src/MixedSchema.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,28 @@ describe('MixedSchema', () => {
66
expect(MixedSchema).toBeDefined()
77
})
88

9+
it('Expose symbol / 1', () => {
10+
expect(MixedSchema()[Symbol.for('fluent-schema-object')]).toBeDefined()
11+
})
12+
13+
it('Expose symbol / 2', () => {
14+
const types = [
15+
S.TYPES.STRING,
16+
S.TYPES.NUMBER,
17+
S.TYPES.BOOLEAN,
18+
S.TYPES.INTEGER,
19+
S.TYPES.OBJECT,
20+
S.TYPES.ARRAY,
21+
S.TYPES.NULL,
22+
]
23+
expect(MixedSchema(types)[Symbol.for('fluent-schema-object')]).toBeDefined()
24+
})
25+
926
describe('factory', () => {
1027
it('without params', () => {
11-
expect(MixedSchema().valueOf()).toEqual({})
28+
expect(MixedSchema().valueOf()).toEqual({
29+
[Symbol.for('fluent-schema-object')]: true,
30+
})
1231
})
1332
})
1433

src/NullSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict'
22
const { BaseSchema } = require('./BaseSchema')
3-
const { setAttribute } = require('./utils')
3+
const { setAttribute, FLUENT_SCHEMA } = require('./utils')
44

55
const initialState = {
66
type: 'null',
@@ -23,6 +23,7 @@ const NullSchema = ({ schema = initialState, ...options } = {}) => {
2323
const { valueOf } = BaseSchema({ ...options, schema })
2424
return {
2525
valueOf,
26+
[FLUENT_SCHEMA]: true,
2627

2728
/**
2829
* Set a property to type null

src/NullSchema.test.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ describe('NullSchema', () => {
66
expect(NullSchema).toBeDefined()
77
})
88

9+
it('Expose symbol', () => {
10+
expect(NullSchema()[Symbol.for('fluent-schema-object')]).toBeDefined()
11+
})
12+
913
describe('constructor', () => {
1014
it('without params', () => {
1115
expect(NullSchema().valueOf()).toEqual({

0 commit comments

Comments
 (0)