npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@kickstartds/jsonschema2netlifycms

v2.8.3

Published

Converts JSON schema to Netlify CMS admin config

Downloads

169

Readme

JSON Schema to Netlify CMS admin config(.yml) converter

This library exports a single function, convert, which converts one or more JSON schemas to a Netlify CMS admin config.yml.

Installation

Install with yarn:

yarn add jsonschema2netlifycms

Usage (TODO Fix, usage differs for Netlify CMS)

You can convert a schema expressed as an object literal:

import { printSchema } from 'graphql'
import convert from 'jsonschema2netlifycms'

const jsonSchema = {
  $id: '#/person',
  type: 'object',
  properties: {
    name: {
      type: 'string',
    },
    age: {
      type: 'integer',
    },
  },
}

const schema = convert({ jsonSchema })

console.log(printSchema(schema))

Output

type Person {
  name: String
  age: Int
}
type Query {
  people: [Person]
}

Alternatively, you can provide the schema as JSON text.

const schema = convert({
  jsonSchema: `{
    "$id": "person",
    "type": "object",
    "properties": {
      "name": {
        "type": "string"
      },
      "age": {
        "type": "integer"
      }
    }
  }`,
})

This will generate the same result as above.

To generate more than one type, provide an array of JSON schemas in either object or text form.

const orange = {
  $id: '#/Orange',
  type: 'object',
  properties: {
    color: {
      type: 'string',
    },
  },
}

const apple = {
  $id: '#/Apple',
  type: 'object',
  properties: {
    color: { type: 'string' },
    bestFriend: {
      $ref: '#/Orange', // <-- reference foreign type using $ref
    },
  },
}

const schema = convert({ jsonSchema: [orange, apple] })

Output

type Apple {
  color: String
  bestFriend: Orange
}
type Orange {
  color: String
}
type Query {
  oranges: [Orange]
  apples: [Apple]
}`

Custom root types (Query, Mutation, Subscription)

By default, a Query type is added to the schema, with one field defined per type, returning an array of that type. So for example, if you have an Person type and a Post type, you'll get a Query type that looks like this:

type Query {
  people: [Person]
  posts: [Posts]
}

(Note that the generated name for the field is automatically pluralized.)

By default, no Mutation or Subscription types are created.

To create a custom Query type and to add Mutation and/or Subscription blocks, provide an entryPoints callback that takes a hash of GraphQL types and returns Query, Mutation (optional), and Subscription (optional) blocks. Each block consists of a hash of GraphQLFieldConfig objects.

const jsonSchema = [log, user, family]

const entryPoints = types => {
  return {
    query: new GraphQLObjectType({
      name: 'Query',
      fields: {
        family: { type: types['Family'] },
        user: {
          type: types['User'],
          args: {
            email: { type: types['Email'] },
          },
        },
      },
    }),
    mutation: new GraphQLObjectType({
      name: 'Mutation',
      fields: {
        stop: { type: types['Log'] },
      },
    }),
  }
}

const schema = convert({ jsonSchema, entryPoints })

See the GraphQL guide "Constructing Types" for more on how to create GraphQL types programmatically.

Note that any types that are not referenced directly or indirectly by your root types will be omitted from the final schema.

JSON schema feature support

Note: This package is designed for use with schemas compliant with JSON Schema Draft 7.

For clarity, the following examples only show the converted types; the Query type is omitted from the output.

Basic types

Input

{
  $id: '#/Person',
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'integer' },
    score: { type: 'number' },
    isMyFriend: { type: 'boolean' },
  },
}

Output

type Person {
  name: String
  age: Int
  score: Float
  isMyFriend: Boolean
}

Array types

Input

const jsonSchema = {
  $id: '#/Person',
  type: 'object',
  properties: {
    name: {
      type: 'string',
    },
    luckyNumbers: {
      type: 'array',
      items: {
        type: 'integer',
      },
    },
    favoriteColors: {
      type: 'array',
      items: {
        type: 'string',
      },
    },
  },
}

Output

type Person {
  name: String
  luckyNumbers: [Int!]
  favoriteColors: [String!]
}

Enums

Input

const jsonSchema = {
  $id: '#/Person',
  type: 'object',
  properties: {
    height: {
      type: 'string',
      enum: ['tall', 'average', 'short'], // <-- enum
    },
  },
}

Output

type Person {
  height: PersonHeight
}

enum PersonHeight {
  tall
  average
  short
}

Required fields

Input

const jsonSchema = {
  $id: '#/Widget',
  type: 'object',
  properties: {
    somethingRequired: { type: 'integer' },
    somethingOptional: { type: 'integer' },
    somethingElseRequired: { type: 'integer' },
  },
  required: ['somethingRequired', 'somethingElseRequired'],
}

Output

type Widget {
  somethingRequired: Int!
  somethingOptional: Int
  somethingElseRequired: Int!
}

Foreign references using $ref

Input

const jsonSchema = {
  {
    $id: '#/Orange',
    type: 'object',
    properties: {
      color: {
        type: 'string',
      },
    },
  },
  {
    $id: '#/Apple',
    type: 'object',
    properties: {
      color: { type: 'string' },
      bestFriend: {
        $ref: '#/Orange', // <-- reference foreign type using $ref
      },
    },
  },
]

Output

type Apple {
  color: String
  bestFriend: Orange
}

type Orange {
  color: String
}

Union types using oneOf

Input

const jsonSchema = {
  {
    $id: '#/Parent',
    type: 'object',
    properties: {
      type: { type: 'string' },
      name: { type: 'string' },
    },
  },
  {
    $id: '#/Child',
    type: 'object',
    properties: {
      type: { type: 'string' },
      name: { type: 'string' },
      parent: { $ref: '#/Parent' },
      bestFriend: { $ref: '#/Person' },
      friends: {
        type: 'array',
        items: { $ref: '#/Person' },
      },
    },
  },
  {
    $id: '#/Person',
    oneOf: [{ $ref: '#/Parent' }, { $ref: '#/Child' }],
  },
]

Output

type Child {
  type: String
  name: String
  parent: Parent
  bestFriend: Person
  friends: [Person!]
}

type Parent {
  type: String
  name: String
}

union Person = Parent | Child

Properties defined as oneOf with if/then are also supported. For example, this:

const jsonSchema = {
  $id: '#/Person',
  oneOf: [
    {
      if: { properties: { type: { const: 'Parent' } } },
      then: { $ref: '#/Parent' },
    },
    {
      if: { properties: { type: { const: 'Child' } } },
      then: { $ref: '#/Child' },
    },
  ],
}

generates the same union type as above:

union Person = Parent | Child

Descriptions for types and fields

Input

const jsonSchema = {
  $id: '#/person',
  type: 'object',
  description: 'An individual human being.',
  properties: {
    name: {
      type: 'string',
      description: 'The full name of the person.',
    },
    age: {
      type: 'integer',
      description: "The elapsed time (in years) since the person's birth.",
    },
  },
}

Output

"""
An individual human being.
"""
type Person {
  """
  The full name of the person.
  """
  name: String
  """
  The elapsed time (in years) since the person's birth.
  """
  age: Int
}

To do

The following features are not currently supported, but I'd like to add them for completeness. Pull requests are welcome.

  • [ ] Combining JSON schemas with allOf, anyOf, or not
  • [ ] Generating GraphQL input types

Acknowledgements

Derived from json-schema-to-graphql-types by Matt Lavin ([email protected]).

Built with typescript-starter by Jason Dreyzehner ([email protected]).

MIT License