Skip to content

Instantly share code, notes, and snippets.

@hediet
Created June 9, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hediet/adaff6f129688465050d2c0a97ef9fe0 to your computer and use it in GitHub Desktop.
Save hediet/adaff6f129688465050d2c0a97ef9fe0 to your computer and use it in GitHub Desktop.
JSON Schema TypeScript Types
export type JsonSchema =
| NumericJsonSchema
| StringJsonSchema
| ArrayJsonSchema
| ObjectJsonSchema
| JsonSchemaReference;
export interface JsonSchemaReference {
$ref: string;
}
export interface BaseJsonSchema {
$id?: string;
$schema?: string;
title?: string;
description?: string;
definitions?: {
[name: string]: JsonSchema;
};
enum?: unknown[];
format?: string | Format;
if?: JsonSchema;
then?: JsonSchema;
else?: JsonSchema;
allOf?: JsonSchema[];
anyOf?: JsonSchema[];
oneOf?: JsonSchema[];
not?: JsonSchema;
}
export type Format =
| typeof Format.dateTime
| typeof Format.date
| typeof Format.time
| typeof Format.email
| typeof Format.idnEmail
| typeof Format.hostname
| typeof Format.idnHostname
| typeof Format.ipv4
| typeof Format.ipv6
| typeof Format.uri
| typeof Format.uriReference
| typeof Format.iri
| typeof Format.iriReference
| typeof Format.uriTemplate
| typeof Format.jsonPointer
| typeof Format.relativeJsonPointer
| typeof Format.regex;
export namespace Format {
export const dateTime = "date-time";
export const date = "date";
export const time = "time";
export const email = "email";
export const idnEmail = "idn-email";
export const hostname = "hostname";
export const idnHostname = "idn-hostname";
export const ipv4 = "ipv4";
export const ipv6 = "ipv6";
export const uri = "uri";
export const uriReference = "uri-reference";
export const iri = "iri";
export const iriReference = "iri-reference";
export const uriTemplate = "uri-template";
export const jsonPointer = "json-pointer";
export const relativeJsonPointer = "relative-json-pointer";
export const regex = "regex";
}
export interface NumericJsonSchema extends BaseJsonSchema {
type: JsonSchemaType.Numeric | JsonSchemaType[];
multipleOf?: number;
maximum?: number;
exclusiveMaximum?: boolean;
minimum?: number;
exclusiveMinimum?: boolean;
}
export interface StringJsonSchema extends BaseJsonSchema {
type: typeof JsonSchemaType.string | JsonSchemaType[];
maxLength?: number;
minLength?: number;
pattern?: string;
}
export interface ArrayJsonSchema extends BaseJsonSchema {
type: typeof JsonSchemaType.array | JsonSchemaType[];
items?: JsonSchema | JsonSchema[];
additionalItems?: JsonSchema;
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;
contains: JsonSchema;
}
export interface ObjectJsonSchema extends BaseJsonSchema {
type: typeof JsonSchemaType.object | JsonSchemaType[];
maxProperties?: number;
minProperties?: number;
required?: string[];
properties?: {
[name: string]: JsonSchema;
};
patternProperties?: {
[name: string]: JsonSchema;
};
additionalProperties?: JsonSchema;
dependencies?: {
[name: string]: JsonSchema | string[];
};
}
namespace JsonSchemaType {
export const number = "number";
export const integer = "integer";
export const array = "array";
export const object = "object";
export const string = "string";
export type Numeric = typeof number | typeof integer;
}
export type JsonSchemaType =
| "null"
| "boolean"
| typeof JsonSchemaType.object
| typeof JsonSchemaType.array
| typeof JsonSchemaType.string
| JsonSchemaType.Numeric;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment