Eidolon
Generate examples and JSON Schema from Refract data structures. Data structures can come from MSON or other input sources.
Given the following MSON attributes from e.g. API Blueprint:
+ Attributes
+ name: Daniel (required) - User's first name
+ age: 10 (required, number) - Age in years
It would generate the following JSON example and JSON Schema:
{
"name": "Daniel",
"age": 10
}
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"required": ["name", "age"],
"properties": {
"name": {
"type": "string",
"description": "User's first name"
},
"age": {
"type": "number",
"description": "Age in years"
}
}
}
Installation & Usage
This project is available via npm
:
npm install eidolon
There are two ways to use the module: either via module-level methods or by instantiating a class instance.
import eidolon, {Eidolon} from 'eidolon';
const input = {"element": "string", "content": "Hello"};
const dataStructures = {};
let example, schema;
example = eidolon.example(input, dataStructures, options);
schema = eidolon.schema(input, dataStructures);
const instance = new Eidolon(dataStructures, options);
example = instance.example(input);
schema = instance.schema(input);
Choose whichever method better suits your use case.
Example & Schema Serialization
Examples and JSON Schema are created as plain Javascript objects. As such, they can be serialized into various formats, such as JSON, YAML, and other more esoteric formats. In the case of JSON Schema, it probably makes the most sense to stick with JSON.
example = instance.example(input);
console.log(JSON.stringify(example, null, 2));
import yaml from 'js-yaml';
console.log(yaml.safeDump(example));
Features
The following features are supported by the example and JSON Schema generators. Note that not all MSON features are supported (yet)!
Example Generator
- Simple types, enums, arrays, objects
- Property descriptions
- References
- Mixins (Includes)
- Arrays with members of different types
- One Of properties (the first is always selected)
JSON Schema Generator
- Simple types, enums, arrays, objects
- Property descriptions
- Required, default, nullable properties
- References
- Mixins (Includes)
- Arrays with members of different types
- One Of (mutually exclusive) properties
Notable Missing Features
The following list of features in no particular order are known to be missing or cause issues. Please feel free to open a pull request with new features and fixes based on this list! wink wink nudge nudge :beers:
- Circular references
- Variable values
- Variable property names
- Variable type names
- Extend element support
- Remote referenced elements (e.g. via HTTP)
- Namespace prefixes
Reference
eidolon.Eidolon([dataStructures], [options])
This class is used to save state between calls to example
and schema
. It is used just like the functions below, except that you pass your data structures to the constructor instead of to each method.
Available options:
Option Name | Description | Default |
---|
defaultValue | Function to generate a default value function (refractElement, path) | Built-in eidolon.defaultValue . |
seed | Seed for the random generator used to create default values | - |
import {Eidolon} from 'eidolon';
const instance = new Eidolon();
const input = {element: 'string', content: 'hello'};
let example = instance.example(input);
let schema = instance.schema(input);
let dereferenced = instance.dereference(input);
eidolon.example(input, [dataStructures], [options])
Generate a new example from the given input refract object and an optional mapping of data structures, where the key is the data structure name and the value is the data structure definition.
Available options are described in the Eidolon class above.
import eidolon from 'eidolon';
const input = {element: 'string', content: 'hello'};
let example = eidolon.example(input);
eidolon.schema(input, [dataStructures])
Generate a new JSON schema from the given input refract object and an optional mapping of data structures, where the key is the data structure name and the value is the data structure definition.
import eidolon from 'eidolon';
const input = {element: 'string', content: 'hello'};
let schema = eidolon.schema(input);
eidolon.dereference(input, [dataStructures])
Dereference an input element or structure of elements with the given data structures. This will return the same element or structure with resolved references so you do not have to handle inheritance or object includes. Each resolved reference will include the name of the referenced type or mixin in the meta.ref
property.
import eidolon from 'eidolon';
const input = {
element: 'MyString'
};
const dataStructures = {
MyElement: {
element: 'string',
meta: {
id: 'MyString'
},
content: 'Hello, world!'
}
};
let dereferenced = eidolon.dereference(input, dataStructures);
console.log(dereferenced.element);
console.log(dereferenced.content);
console.log(dereferenced.meta.ref);
eidolon.inherit(base, element)
Generate a new element with merged properties from both base
and element
, taking care to prevent duplicate members. This utility can be used when traversing the element tree.
import eidolon from 'eidolon';
const base = {
element: 'number',
meta: {
id: 'NullableNumber',
default: 0
},
attributes: {
typeAttributes: ['nullable']
}
};
const element = {
element: 'NullableNumber',
attributes: {
default: 2
},
content: 10
}
let merged = eidolon.inherit(base, element);
{
element: 'number',
meta: {
ref: 'NullableNumber',
links: [
{
relation: 'origin',
href: 'http://refract.link/inherited/'
}
]
},
attributes: {
default: 2,
typeAttributes: ['nullable']
},
content: 10
}
License
Copyright © 2016 Daniel G. Taylor
http://dgt.mit-license.org/