Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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))
```