Git Product home page Git Product logo

schema-type's Introduction

Schema Type

Get TypeScript Type from JSON Schema like object!

Source

Everything is in a single file: index.ts

Usage

See and test playground.ts

Example

import { SchemaType, Packed } from 'schema-type';

const defs = {
    User: {
        type: 'object',
        required: ['id', 'username', 'description'],
        properties: {
            id: { type: 'string' },
            username: { type: 'string' },
            description: { type: 'string', nullable: true },
        }
    },
    Post: {
        type: 'object',
        required: ['id', 'userId', 'user', 'title', 'contens', 'state'],
        properties: {
            id: { type: 'string' },
            userId: { type: 'string' },
            user: { type: 'object', ref: 'User' },
            title: { type: 'string', nullable: true },
            contens: { type: 'string' },
            state: { type: 'string', enum: ['published', 'draft'] },
        }
    },
    PostOrUser: {
        oneOf: [
            { type: 'object', ref: 'Post' },
            { type: 'object', ref: 'User' },
        ]
    }
} as const;

// Get the type of `Post` entity
type Post = Packed<typeof defs, 'Post'>; 

// Define like schema
const likeDef = {
    type: 'object',
    required: ['id', 'userId', 'user', 'postId', 'post'],
    properties: {
        id: { type: 'string' },
        userId: { type: 'string' },
        user: { type: 'object', ref: 'User' },
        postId: { type: 'string' },
        post: { type: 'object', ref: 'Post' },
    }
} as const;

// Get like entity type
type Like = SchemaType<typeof defs, typeof likeDef>

SchemaType<Ctx, Schema>

Just get the type from JSON Schema like object.

Ctx

You should Ctx, implementation of following type named Defs, the list of defs:

export type Defs = { [x: string]: { type: 'object'; properties: Obj<Defs>; } | { oneOf: Schema<Defs>[]; } | { allOf: Schema<Defs>[]; }; };

If you don have no def, write {} (empty object).

Packed<Ctx, keyof Ctx>

Look up the def type with its name.

(The origin of this type name is a remnant from when it was used in Misskey.)

schema-type's People

Contributors

tamaina avatar

Stargazers

syuilo avatar

Watchers

James Cloos avatar  avatar  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.