Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bali182/json-schema-visitor
Visitor for inspecting JSON schemas
https://github.com/bali182/json-schema-visitor
json json-schema schema visitor
Last synced: about 4 hours ago
JSON representation
Visitor for inspecting JSON schemas
- Host: GitHub
- URL: https://github.com/bali182/json-schema-visitor
- Owner: bali182
- Created: 2017-03-24T11:04:15.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-04-07T16:57:13.000Z (almost 8 years ago)
- Last Synced: 2024-11-16T17:03:50.945Z (2 months ago)
- Topics: json, json-schema, schema, visitor
- Language: JavaScript
- Size: 13.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# json-schema-visitor [![Build Status](https://travis-ci.org/bali182/json-schema-visitor.svg?branch=master)](https://travis-ci.org/bali182/json-schema-visitor)
A package for inspecting JSON schemas.
## Supported schema types
These directly translate to the method names you need to supply to the configuration object passed to `createVisitor`:
- `object`
- `array`
- `enum`
- `boolean`
- `number`
- `null`
- `string`
- `oneOf`
- `anyOf`
- `allOf`
- `ref`
- `unknown`## Usage
### Simple example
```js
import createVisitor from 'json-schema-visitor'const visitor = createVisitor({
object(schema) {
return `An object schema: ${JSON.stringify(schema)}`
}
array(schema) {
return `An array schema: ${JSON.stringify(schema)}`
}
})// 2 cases handled
expect(visitor({ type: 'object' })).toBe('An object schema: {type:"object"}')
expect(visitor({ type: 'array' })).toBe('An array schema: {type:"array"}')// All other cases
expect(visitor({ type: 'string' })).toBeUndefined()
expect(visitor(null)).toBeUndefined()
expect(visitor('foo')).toBeUndefined()
```### Default visit method
You don't have to specify each visit method. You can have a fallback method named `any` which handles all unhandled cases.
```js
const visitor = createVisitor({
object(schema) {
return 'An object schema'
},
any(schema) {
return 'Everything else'
}
})// Case handled
expect(visitor({ type: 'object' })).toBe('An object schema')// All other cases
expect(visitor({ type: 'string' })).toBe('Everything else')
expect(visitor(null)).toBe('Everything else')
expect(visitor('foo')).toBe('Everything else')
```### Extra arguments
You can pass extra arguments to the visitor. These arguments will be paseed as second, third, etc. arguments to each visit method.
```js
const visitor = createVisitor({
object(schema, someArg, otherArg) {
return `An object schema. Also args: ${someArg} and ${otherArg}`
}
})expect(visitor({ type: 'object' }, 'foo', 42)).toBe('An object schema. Also args: foo and 42')
```### Recursive traversing
This visitor recursively visits every element of the schema, and applies a callback on them.
```js
const visitor = createVisitor({
object(schema, callback) {
callback(schema)
Object.keys(schema.properties)
.map(key => schema.properties[key])
.forEach(childSchema => visitor(childSchema, callback))
}
array(schema, callback) {
callback(schema)
visitor(schema.items, callback)
},
allOf(schema, callback) {
callback(schema)
schema.allOf.forEach(childSchema => visitor(childSchema, callback))
},
anyOf(schema, callback) {
callback(schema)
schema.anyOf.forEach(childSchema => visitor(childSchema, callback))
},
oneOf(schema, callback) {
callback(schema)
schema.oneOf.forEach(childSchema => visitor(childSchema, callback))
}
any(schema, callback) {
callback(schema)
}
})visitor(someSchema, schema => console.log(schema))
```