GithubHelp home page GithubHelp logo

joe-chimienti / json-schema-to-case-class Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cchandurkar/json-schema-to-case-class

0.0 1.0 0.0 10.09 MB

A library that converts JSON Schema to Scala Case Classes

License: MIT License

Shell 0.34% TypeScript 94.91% JavaScript 4.75%

json-schema-to-case-class's Introduction

json-schema-to-case-class

A library to convert complex JSON Schema to Scala Case Classes. Supports both NodeJs and Browser environments.
Try Online Editor.

Build Status npm version License PRs Welcome PRs Welcome

JSON Schema Generated Scala Case Classes
{
  "title": "Product",
  "type": "object",
  "required": [ "productId", "productName", "price" ],
  "properties": {
    "productId": { "type": "integer" },
    "productName": { "type": "string" },
    "price": { "type": "number" },
    "tags": {
      "type": "array",
      "items": { "type": "string" }
    },
    "dimensions": {
      "type": "object",
      "properties": {
        "length": { "type": "number" },
        "width": { "type": "number" },
        "height": { "type": "number" }
      }
    }
  }
}
case class Product (
   productId: Integer,
   productName: String,
   price: Double,
   tags: Option[List[String]],
   dimensions: Option[Dimensions]
)
case class Dimensions (
   length: Double,
   width: Double,
   height: Double
)

Features

  1. Resolve local as well as remote schema references.
  2. Handle nested JSON schema objects.
  3. Can wrap optional fields in Option[].
  4. Support constraint validations through assertion.
  5. Generate scaladoc based on the description provided in JSON Schema.
  6. Support various text cases for case class and parameter names.
  7. Can default to provided generic type. (default Any)
  8. Can build Enumeration objects for properties with enum field.

Installing

npm install --save json-schema-to-case-class

NPM

Usage

For NodeJs (TypeScript):

import { convert, IConfig } from 'json-schema-to-case-class'

const mySchema: any = { ... };
const config: IConfig = { ... };  // <-- Optional

// With default configuration:
convert( mySChema )
  .then( result => console.log(result) )
  .catch( err => console.error(err) )

// With custom configuration:
convert( mySchema, config )
  .then( result => console.log(result) )
  .catch( err => console.error(err) )

For NodeJs (JavaScript):

const { convert } = require('json-schema-to-case-class');
// OR
const SchemaConverter = require('json-schema-to-case-class');
SchemaConverter.convert( , );

For browser: If you are using the prebuild bundle, it has all the APIs under SchemaConverter global object:

SchemaConverter.convert( mySchema, config )

Configuration

It is optional to pass configuration object. Every configuration setting is optional as well. When not passed, default kicks-in. IConfig:

Name Type Description Default
maxDepth number Parses nested schema objects upto the depth = maxDepth. Pass 0 for no limit on depth. 0
optionSetting string Setting to wrap optional parameters in Option[].
1. "useOptions" - Wrap optional parameters in Option[]
2. "useOptionsForAll" - Wrapp all parameters in Option[]
3. "noOptions" - Don't wrap any parameter in Option[]
useOptions
classNameTextCase string Text case for case class title. Could be one of:
camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase
pascalCase
classParamsTextCase string Text case for case class parameters. Could be one of above. snakeCase
topLevelCaseClassName string This config kicks-in only if top level schema object does not have 'title' property. MyCaseClass
defaultGenericType string Case class parameter type when type information not available from the schema or if we are converting partial schema using maxDepth setting. Any
parseRefs boolean Whether to resolve the local or remote schema references ($ref). true
generateComments boolean Whether to generate scaladoc-like comments for the case class generated. false
generateValidations boolean Whether to generate validations in the form of assertions in case class body. false
generateEnumerations boolean Whether to generate enumerations for enum fields. It generates an object extending scala's Enumeration class and use it in parameter type. false

Browser Support

This library supports recent versions of every major web browsers. Refer to the browserified build dist/js2cc.min.js that you can directly use in <script /> tag of HTML page. It already bundles all the required polyfills.

Limitations

As of now, it does not support a few latest JSON Schema features such as allOf/ anyOf/ oneOf.

Contributing

All contributions, enhancements, and bug-fixes are welcome. Open an issue or create a pull request.

Short-term Goals
  1. Handle allOf/ anyOf/ oneOf.
  2. Support validations on class parameters. Eg. exclusiveMinimum, minItems, uniqueItems.
  3. Add support for converting YAML.
  4. Update online editor to add more examples to choose from.
Building locally
  1. Clone the repo
    https://github.com/cchandurkar/json-schema-to-case-class.git

  2. Install Dependancies
    npm install

  3. Setup development environment
    npm run setup-dev-env

  4. Run the test
    npm test

  5. Run eslint checks
    npm run lint

  6. Run eslint checks and fix the errors
    npm run lint:fix

json-schema-to-case-class's People

Contributors

cchandurkar avatar

Watchers

James Cloos avatar

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.