0.0.37 • Published 10 months ago

json-schema-to v0.0.37

Weekly downloads
28
License
MIT
Repository
github
Last release
10 months ago

Build status NPM version Coverage Status Dependency Status devDependency Status

JSON-Schema To ≤GraphQL|Protobuf|Code≥.™

Generate third-party definitions directly from your JSON-Schema.

Contribute!

This module is under development, we're missing:

  • Documentation
  • Advanced examples
  • TypeScript support
  • Complete unit-testing
  • Complete code-coverage

How it works?

JSON-Schema, GraphQL and Protobuf are generated from YAML definitions, e.g.

id: User
service:
  calls:
  - set: updatePassword
    resp: User
    input: UpdatePasswordParams
    params: UpdatePasswordRequest
definitions:
  UpdatePasswordParams:
  UpdatePasswordRequest:

This definition is also a JSON-Schema definition.

Actually, it can be a .json file to but YAML format it's easier to maintain.

Due resp, input and params are schema identifiers they'll be looked-up from all available schemas or from #/definitions on the current file.

Options for calls are:

  • get RPC call (or Query)
  • set RPC call (or Mutation)
  • resp Response type
  • input Request type
  • params Request type (Protobuf only)

Use the params option only if you want different input types.

Having some definitions like this we can produce different outputs, e.g.

$ tree .
.
└── schema
    └── test.yml

1 directory, 1 file

$ npx json-schema-to -s schema --json --graphql --protobuf
write ./generated/user.json
write ./generated/user.gql
write ./generated/user.proto
write ./generated/common.json
write ./generated/common.gql
write ./generated/common.proto

Now you can use those sources in your application.

Use --help to display more usage info from the CLI

TypeScript

We're able to produce .d.ts files as well.

Types, enums and interfaces are exported together in a single entry-point:

import type { User, Success } from './generated';

const ok: Success = { success: true };
const user: User = { email: 'a@b.c', role: 'USER' };

console.log(ok);
console.log(user);

Enumerations

The generated index.js script exports a function that can be called to augment any object with the exported enums:

// main/index.js
require('../generated')(module.exports = {
  // other stuff
});

Later, just import your wrapped module and use the available enums, e.g.

// test.ts
import { someEnum } from './main';

const value: someEnum = someEnum.SOME_VALUE;

If you have a ./main/index.d.ts file any used enum will be type-checked in your script.

Supported keywords

Currently, a small subset of keywords from JSON-Schema v4 are supported:

  • id Used to declare types or services, it MUST be unique
  • $ref Dereferencing is resolved against defined refs only
  • enum Fixed set of values to enumerate, strings only
  • type Declare the used type of any given definition
  • items Standard definition of repeated objects, array will not work
  • required List of required properties to declare in the generated types
  • properties Standard set of properties from a given object, they are the type props
  • definitions Additional types to export, if no id is given then its basename will be used

⚠ More keywords can be implemented later, by now complete support is no a requirement.

0.0.37

10 months ago

0.0.35

10 months ago

0.0.36

10 months ago

0.0.33

1 year ago

0.0.34

1 year ago

0.0.32

2 years ago

0.0.30

2 years ago

0.0.31

2 years ago

0.0.29

3 years ago

0.0.28

3 years ago

0.0.26

3 years ago

0.0.27

3 years ago

0.0.25

3 years ago

0.0.24

4 years ago

0.0.23

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.19

4 years ago

0.0.18

5 years ago

0.0.17

5 years ago

0.0.16

5 years ago

0.0.15

5 years ago

0.0.14

5 years ago

0.0.13

5 years ago

0.0.12

5 years ago

0.0.11

5 years ago

0.0.10

5 years ago

0.0.9

5 years ago

0.0.8

5 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago