igata
v1.3.11
Published
Converts a JSON Schema to Flow type definition
Downloads
6
Maintainers
Readme
Igata
Convert a JSON Schema to a Flow type definition.
Installation
npm install --save igata
Usage
import {convert} from 'igata';
convert({$id: 'String', type: 'string'});
// => export type String = string;
convert({$id: 'Enum', enum: [1, 2]});
// => export type Enum = 1 | 2;
convert({$id: 'Object', type: 'object', properties: {foo: {type: 'string'}}});
// => export type Object = {\n foo?: string\n};
convert({$id: 'Array', type: 'array', items: {type: 'string'}});
// => export type Array = string[];
convert({$id: 'Tuple', type: 'array', items: [{type: 'string'}, {type: 'number'}]});
// => export type Tuple = [string, number];
convert({$id: 'Union', type: ['string', 'number']});
// => export type Union = string | number;
convert({$id: 'ComplexUnion', anyOf: [{type: 'number'}, {type: 'object', properties: {foo: {type: 'string'}}}]});
// => export type ComplexUnion = number | {\n foo?: string\n};
Following example is a nullable type.
const jsonSchema = {
$id: 'Nullable',
type: ['string', 'null'],
};
convert(jsonSchema);
// => export type Nullable = string | null;
Following example is an exact object type.
const jsonSchema = {
$id: 'ExactObject',
type: 'object',
additionalProperties: false,
properties: {
foo: {
type: 'string',
},
},
};
convert(jsonSchema);
// => export type ExactObject = {|\n foo?: string\n|};
Following example is a reference to a definition using $ref
.
const jsonSchema = {
$id: 'Definition',
$ref: '#/definitions/foo',
definitions: {
foo: {
type: 'object',
properties: {
bar: {
type: 'string',
},
},
},
},
};
convert(jsonSchema);
// => export type Definition = {\n bar?: string\n};
Test
npm run test
Demo
https://igata.netlify.com/
License
MIT license.