Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/hoseungme/schema

JSON schema builder for TypeScript
https://github.com/hoseungme/schema

json-schema open-api typescript

Last synced: 1 day ago
JSON representation

JSON schema builder for TypeScript

Awesome Lists containing this project

README

        

Schema

JSON schema builder for TypeScript




## Install

```
$ npm install @hoseung.json/schema
```

## Usage

```typescript
import { S } from "@hoseung.json/schema";

// {
// type: "object",
// properties: {
// id: {
// type: "number"
// },
// name: {
// type: "string"
// }
// }
// required: [
// "id",
// "name"
// ]
// }
const User = S.Object({
id: S.Number(),
name: S.String(),
});

// {
// id: number;
// name: string;
// }
type User = S.Resolve;

if (User.match(unknownValue)) {
// type of unknownValue is:
// {
// id: number;
// name: string;
// }
}
```

## Contents

- [JSON Schema](#json-schema)
- [Number()](#number)
- [String()](#string)
- [Boolean()](#boolean)
- [Null()](#null)
- [Array()](#array)
- [Object()](#object)
- [Literal()](#literal)
- [Optional()](#optional)
- [Union()](#union)
- [Dict()](#dict)
- [Validate Schema](#validate-schema)
- [Match Value](#match-value)

## JSON Schema

You can create a JSON schema using functions below.

### Number()

A function which creates a number JSON schema

```typescript
// {
// type: "number"
// }
const Num = S.Number();

// number
type Num = S.Resolve;
```

### String()

A function which creates a string JSON schema

```typescript
// {
// type: "string"
// }
const Str = S.String();

// string
type Str = S.Resolve;
```

### Boolean()

A function which creates a boolean JSON schema

```typescript
// {
// type: "boolean"
// }
const Bool = S.Boolean();

// boolean
type Bool = S.Resolve;
```

### Null()

A function which creates a null JSON schema

```typescript
// {
// type: "null"
// }
const Null = S.Null();

// null
type Null = S.Resolve;
```

### Array()

A function which creates an array JSON schema

```typescript
// {
// type: "array",
// items: {
// type: "number"
// }
// }
const Array = S.Array(S.Number());

// number[]
type Array = S.Resolve;
```

### Object()

A function which creates an object JSON schema

```typescript
// {
// type: "object",
// properties: {
// a: {
// type: "number"
// },
// b: {
// type: "string"
// }
// },
// required: [
// "a",
// "b"
// ]
// }
const Obj = S.Object({
a: S.Number(),
b: S.String(),
});

// {
// a: number;
// b: string;
// }
type Obj = S.Resolve;
```

### Literal()

A function which creates a number/string/boolean JSON schema with constant value

```typescript
// {
// type: "number",
// const: "foo"
// }
const Literal = S.Literal(123);

// 123
type Literal = S.Resolve;
```

```typescript
// {
// type: "string",
// const: "foo"
// }
const Literal = S.Literal("foo");

// "foo"
type Literal = S.Resolve;
```

```typescript
// {
// type: "boolean",
// const: true
// }
const Literal = S.Literal(true);

// true
type Literal = S.Resolve;
```

### Optional()

A function which marks a property in an object JSON schema as optional

```typescript
// {
// type: "object",
// properties: {
// a: {
// type: "number"
// },
// b: {
// type: "string"
// }
// },
// required: [
// "a"
// ]
// }
const Optional = S.Object({
a: S.Number(),
b: S.Optional(S.String()),
});

// {
// a: number;
// b?: string;
// }
type Optional = S.Resolve;
```

### Union()

A function which creates an anyOf JSON schema

```typescript
// {
// anyOf: [
// {
// type: "number"
// },
// {
// type: "string"
// }
// ]
// }
const Union = S.Union([S.Number(), S.String()]);

// number | string
type Union = S.Resolve;
```

### Dict()

A function which creates an object JSON schema only with additionalProps

```typescript
// {
// type: "object",
// properties: {},
// additionalProperties: {
// type: "number"
// }
// }
const Dict = S.Dict(S.Number());

// {
// [key: string]: number;
// }
type Dict = S.Resolve;
```

## Validate Schema

You may need to validate what kind of a Schema is. So there are validation function which named `is{Schema Name}` for [all kinds of Schema](#json-schema).

```typescript
S.isNumber(S.Number()); // true
S.isNumber(S.String()); // false

if (S.isNumber(unknownSchema)) {
// type of unknownSchema is NumberSchema
}
```

## Match Value

You may need to validate if a value matches a Schema. So [all kinds of Schema](#json-schema) have `match` function.

```typescript
const Locale = S.Union([S.Literal("ko"), S.Literal("en")]);

if (Locale.match(unknownValue)) {
// type of unknownValue is "ko" | "en"
}
```