Giter Club home page Giter Club logo

tson-schema's Introduction

tson-schema

This project aims to bring you an easy way to create json-schemas and TypeScript types using a single API. This API is kept as close as possible to json-schema so you don't have to worry about learning yet another API. Support for new json-schema versions and new TypeScript versions is added on a best effort basis.

This is a work in progress! Known missing json-schema features include:

  • array.additionalItems
  • object.additionalProperties
  • $ref
  • JSON-Schema conditional schemas (if/else)
  • Limited TypeScript support for:
    • big enum schemas
    • big allOf schemas

Installing

npm install -S tson-schema

Or

yarn add tson-schema

Usage

import * as t from 'tson-schema'

/**
 * Array
 */
const numberArraySchema = t.array({
  items: t.number({
    minimum: 1
  }),
  minItems: 2,
  uniqueItems: true
})

numberArraySchema.getSchema() // { type: 'array', items: { type: 'number', minimum: 1 }, minItems: 2, uniqueItems: true }
numberArraySchema.type        // number[]

/**
 * Object
 */
const objectSchema = t.object({
  properties: {
    req: t.string(),
    opt: t.tuple({
      items: [t.integer()]
    })
  },
  required: ['req']
})

objectSchema.getSchema() // { type: 'object', properties: { req: { type: 'string' }, opt: { type: 'array', items: [{ type: 'integer' }] } }, required: ['req'] }
objectSchema.type        // { req: 'string', opt?: [number] }

/**
 * Enum
 */
const enumSchema = t.enum(['A', 2, 'C', 4])

enumSchema.getSchema() // { enum: ['A', 2, 'C', 4] }
enumSchema.type        // 'A' | 2 | 'C' | 4

/**
 * anyOf
 */
const anyOfSchema = t.anyOf([
  s.const('A'),
  s.integer()
])

enumSchema.getSchema() // { anyOf: [{ const: 'A' }, { type: 'integer' }] }
enumSchema.type        // 'A' | number

tson-schema's People

Contributors

mjwwit avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

woutwo

tson-schema's Issues

Improve method names of API

Currently, the library uses capitalized names close to or even the same as JSON-Schema type values. This should be brought closer to the JSON-Schema API.

Make object properties and required optional

The library currently incorrectly marks the properties and required properties of the object type as required, while they are optional in json-schema. Figure out a way to make them optional while retaining the current functionality.

Add json-schema annotation metadata properties

JSON-Schema defines a set of generic keywords used to give meaning and or contextual information to users of the schema. This library shouldn't do anything functional with those properties, but they should be allowed properties for all types.

The properties: are title, description, default, examples, and $comment

Implement support for enums

JSON-Schema supports the use of enums to allow a limited number of values to pass schema validation and TypeScript supports a very similar feature. Implement enum in a way that stays as close as possible to the JSON-Schema API.

Add support for object additionalProperties

The json-schema object type allows you to restrict the type of additional properties in an object, as documented here. TypeScript allows a similar feature (object index signatures). This should be implemented as close as possible to the json-schema API.

Rewrite in rust

The performance is sub-par compared to what this could have been if it was written in Rust. Maybe you could rewrite it and then call the binary??

Improve documentation

Documentation should reside in the README (for now) and contain an extensive API reference including code samples and gotchas.

Additionally, there should be a section with common cases/issues and their recommended solutions.

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.