GithubHelp home page GithubHelp logo

raisely / sequelize-to-json-schema Goto Github PK

View Code? Open in Web Editor NEW
4.0 7.0 3.0 31 KB

Flexible json-schema generator from sequelize models

License: Other

JavaScript 100.00%
json-schema sequelize-models schema-generation

sequelize-to-json-schema's Introduction

sequelize-to-json-schema

Flexible json-schema generator from sequelize models

Build Status

Features:

  • Rename attributes if they are different in your json schema (eg snake_case to camelCase)
  • Include associations using $ref or inline
  • Automatically generate examples for enum
  • Easily customise your schema with descriptions, examples, validation, etc
  • Built for Draft 06

Example

// User model is defined as
// userDefinition = {
//   full_name: Sequelize.STRING,
//   status: {
//     type: Sequelize.ENUM,
//     values: ['REAL', 'IMAGINED'],
//   },
// }
// With associations for hasMany addresses and hasOne profile

const schemaFactory = require('sequelize-to-json-schema');

const factory = new SchemaFactory({
  customSchema: {
    // modelName: { attributeName: { <schema> } }
    user: { status: { description: 'Was it all just a dream?' } },
  }
  hrefBase: 'http://schema.example',
});
const schemaGenerator = factory.getSchemaGenerator(User);
const schema = schemaGenerator.getSchema();

// Results in
schema = {
  {
    title: 'User',
    '$id': 'http://schema.example/user.json',
    type: 'object',
    '$schema': 'http://json-schema.org/draft-06/schema#',
    properties: {
      full_name: {
        '$id': '/properties/full_name',
        type: 'string',
        examples: [],
        title: 'Full name'
      },
      status: {
        '$id': '/properties/status',
        type: 'string',
        examples: ['REAL', 'IMAGINED'],
        enum: ['REAL', 'IMAGINED'],
        title: 'Status',
        description: 'Was it all just a dream?'
      }
    }
  }
}

You can customise your factory by passing options

options.association

An object listing the associations to include in the schema for all models The keys of the objects are the names of models, each key selects an object where the keys of the object are the association names and the value should be either 'INLINE' or 'REL'. Use 'INLINE' if you want the association to be presented within the schema instead of by reference ('REL')

options.customSchema

An object defineing custom information to be placed in the schema. Top level keys are model names, which contain a objects who's keys are attribute or association names, and then custom keys to place in your schema for that attribute.

options.hrefBase

The base to use for any references

options.jsonAssociationMapper

A function for mapping association names, it is of the form jsonAssociationMapper(model, attributeName) It must return an array of the form [modelAttribute, jsonAttribute] This is useful if your associations are named differently in your models to how they are presented in JSON. (eg pluralization or snake_case to camelCase)

If it is unspecified, the schema generator will simply use association names unchanged

Note This function must return [false, false] if the given property is not an association

options.jsonAttributeMapper

A function for mapping attribute names in the model to json schema attributes

eg

jsonAttributeMapper = (modelAttr) => toCamelCase(modelAttr);

options.selectAttributes

Selects which attributes to describe in the schema for a given model selectAttributes(model) This must return an array of strings, where each entry in the array is an attribute. Use this to prevent all attributes being described by the schema

options.virtualProperties

Add virtual properties - properties that are not present in the model, but are generated dynamically whenever a model is converted to JSON. These take the form of customSchema, but must include the attribute type which contains a string representation of the Sequelize type that the property would be if it were "real"

const options = {
  virtualProperties: {
    users: {
      postCount: { type: 'INTEGER', description: 'Number of posts by the user' },
    },
  },
}

Advanced example

const factory = new SchemaFactory({
  customSchema,
  jsonAttributeMapper = (attr) => _.camelCase(attr),
  selectAttributes = () => ['full_name', 'status', 'address', 'profile'],
  associations: { user: { address: 'inline' } },
  hrefBase: 'http://schema.example',
});
const schemaGenerator = factory.getSchemaGenerator(user);
const const schema = schemaGenerator.getSchema();

schema = {
  title: 'User',
  '$id': 'schema.example/user.json',
  type: 'object',
  '$schema': 'http://json-schema.org/draft-06/schema#',
  properties: {
    address: { type: 'array', items: [Object], title: 'Address' },
    fullName: {
      '$id': '/properties/fullName',
      type: 'string',
      examples: [],
      title: 'Full name'
    },
    profile: { '$ref': 'http://schema.example/profile.json', title: 'Profile' },
    status: {
      '$id': '/properties/status',
      type: 'string',
      examples: ['REAL', 'IMAGINED'],
      enum: ['REAL', 'IMAGINED'],
      title: 'Status',
    }
  }
};

Contributing

Contributions are welcome. Please submit a pull request and include tests.

Please follow the coding style in .editorconfig and .eslintrc.

Contributions should pass npm run test:ci && npm run lint (see below on testing)

Testing

Run npm test

License

This software is licensed under the Just World License.

sequelize-to-json-schema's People

Contributors

chrisjensen avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

sequelize-to-json-schema's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.