Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-schema-to-ts

Package Overview
Dependencies
Maintainers
1
Versions
81
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-schema-to-ts - npm Package Compare versions

Comparing version 1.6.5 to 2.0.0-beta.0

documentation/FAQs/can-i-assign-jsonschema-to-my-schema-and-use-fromschema-at-the-same-time.md

3

lib/definitions/index.d.ts

@@ -1,1 +0,2 @@

export { JSONSchema6DefinitionWithoutInterface } from "./jsonSchema6";
export { JSONSchema7 } from "./jsonSchema7";
export { FromSchemaOptions, FromSchemaDefaultOptions, } from "./fromSchemaOptions";

@@ -1,11 +0,16 @@

import { A, O } from "ts-toolbelt";
import { JSONSchema6Definition } from "json-schema";
import { JSONSchema6DefinitionWithoutInterface } from "./definitions";
import { Resolve } from "./meta-types";
import { ParseSchema } from "./parse-schema";
import { M } from "ts-algebra";
import { O } from "ts-toolbelt";
import type { JSONSchema7 as $JSONSchema7, FromSchemaOptions, FromSchemaDefaultOptions } from "./definitions";
import type { ParseSchema } from "./parse-schema";
import type { Readonly, Writable } from "./utils";
export { FromSchemaOptions, FromSchemaDefaultOptions } from "./definitions";
/**
* Unwided JSON schema (e.g. defined with the `as const` statement)
* Unwidened V7 JSON schema (e.g. defined with the `as const` statement)
*/
export declare type JSONSchema = JSONSchema6Definition | boolean | O.Readonly<Exclude<JSONSchema6DefinitionWithoutInterface, boolean>, A.Key, "deep">;
export declare type JSONSchema7 = $JSONSchema7 | Readonly<Extract<$JSONSchema7, O.Object>>;
/**
* Unwidened JSON schema (e.g. defined with the `as const` statement)
*/
export declare type JSONSchema = JSONSchema7;
/**
* Given a JSON schema defined with the `as const` statement, infers the type of valid instances

@@ -15,2 +20,5 @@ *

*/
export declare type FromSchema<S extends JSONSchema> = Resolve<ParseSchema<S extends object ? O.Writable<S, A.Key, "deep"> : S>>;
export declare type FromSchema<S extends JSONSchema7, O extends FromSchemaOptions = FromSchemaDefaultOptions> = M.$Resolve<ParseSchema<S extends O.Object ? Writable<S> : S, {
parseNotKeyword: O["parseNotKeyword"] extends boolean ? O["parseNotKeyword"] : FromSchemaDefaultOptions["parseNotKeyword"];
parseIfThenElseKeywords: O["parseIfThenElseKeywords"] extends boolean ? O["parseIfThenElseKeywords"] : FromSchemaDefaultOptions["parseIfThenElseKeywords"];
}>>;

@@ -0,11 +1,15 @@

import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { Any, Intersection } from "../meta-types";
import { Get, HasKeyIn } from "../utils";
import { ParseSchema } from ".";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema } from "./utils";
export declare type ParseAllOfSchema<S> = RecurseOnAllOfSchema<Get<S, "allOf">, S, HasKeyIn<S, "enum" | "const" | "type" | "anyOf" | "oneOf"> extends true ? ParseSchema<Omit<S, "allOf">> : Any>;
declare type RecurseOnAllOfSchema<V, S, R> = {
export declare type AllOfSchema = JSONSchema7 & {
allOf: JSONSchema7[];
};
export declare type ParseAllOfSchema<P extends AllOfSchema, O extends ParseSchemaOptions> = RecurseOnAllOfSchema<P["allOf"], P, O, HasKeyIn<P, "enum" | "const" | "type" | "anyOf" | "oneOf"> extends true ? ParseSchema<Omit<P, "allOf">, O> : M.Any>;
declare type RecurseOnAllOfSchema<S extends JSONSchema7[], P extends AllOfSchema, O extends ParseSchemaOptions, R extends any> = {
stop: R;
continue: V extends L.List ? RecurseOnAllOfSchema<L.Tail<V>, S, Intersection<ParseSchema<MergeSubSchema<Omit<S, "allOf">, L.Head<V>>>, R>> : never;
}[V extends [any, ...L.List] ? "continue" : "stop"];
continue: RecurseOnAllOfSchema<L.Tail<S>, P, O, M.$Intersect<ParseSchema<MergeSubSchema<Omit<P, "allOf">, L.Head<S>>, O>, R>>;
}[S extends [any, ...any[]] ? "continue" : "stop"];
export {};

@@ -0,11 +1,15 @@

import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { Intersection, Union } from "../meta-types";
import { Get, HasKeyIn, Merge } from "../utils";
import { ParseSchema } from ".";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn, Merge } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema, RemoveInvalidAdditionalItems } from "./utils";
export declare type ParseAnyOfSchema<S> = Union<RecurseOnAnyOfSchema<Get<S, "anyOf">, S>>;
declare type RecurseOnAnyOfSchema<S, P, R = never> = {
export declare type AnyOfSchema = JSONSchema7 & {
anyOf: JSONSchema7[];
};
export declare type ParseAnyOfSchema<S extends AnyOfSchema, O extends ParseSchemaOptions> = M.$Union<RecurseOnAnyOfSchema<S["anyOf"], S, O>>;
declare type RecurseOnAnyOfSchema<S extends JSONSchema7[], P extends AnyOfSchema, O extends ParseSchemaOptions, R extends any = never> = {
stop: R;
continue: S extends L.List ? RecurseOnAnyOfSchema<L.Tail<S>, P, R | (HasKeyIn<P, "enum" | "const" | "type"> extends true ? Intersection<ParseSchema<Omit<P, "anyOf">>, ParseSchema<MergeSubSchema<Omit<P, "anyOf">, L.Head<S>>>> : ParseSchema<Merge<Omit<P, "anyOf">, RemoveInvalidAdditionalItems<L.Head<S>>>>)> : never;
}[S extends [any, ...L.List] ? "continue" : "stop"];
continue: RecurseOnAnyOfSchema<L.Tail<S>, P, O, R | (HasKeyIn<P, "enum" | "const" | "type"> extends true ? M.$Intersect<ParseSchema<Omit<P, "anyOf">, O>, ParseSchema<MergeSubSchema<Omit<P, "anyOf">, L.Head<S>>, O>> : ParseSchema<Merge<Omit<P, "anyOf">, RemoveInvalidAdditionalItems<L.Head<S>>>, O>)>;
}[S extends [any, ...any[]] ? "continue" : "stop"];
export {};

@@ -1,14 +0,32 @@

import { A, L } from "ts-toolbelt";
import { Arr, Tuple, Union, Error } from "../meta-types";
import { DoesExtend, Get, IsObject } from "../utils";
import { ParseSchema } from ".";
export declare type ParseArrSchema<S> = "items" extends keyof S ? IsObject<S["items"]> extends true ? Arr<ParseSchema<S["items"]>> : S["items"] extends L.List ? Union<FromTreeTuple<ParseTuple<A.Cast<S["items"], L.List>>, S>> : Error<'Invalid value in "items" property'> : Arr;
export declare type ParseTuple<S extends L.List, R extends L.List = []> = {
import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { JSONSchema7 } from "../definitions";
import { DoesExtend } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
export declare type ArraySchema = JSONSchema7 & {
type: "array";
};
export declare type SimpleArraySchema = JSONSchema7 & {
type: "array";
items: JSONSchema7;
};
export declare type TupleSchema = JSONSchema7 & {
type: "array";
items: JSONSchema7[];
};
export declare type ParseArraySchema<S extends ArraySchema, O extends ParseSchemaOptions> = S extends SimpleArraySchema ? M.$Array<ParseSchema<S["items"], O>> : S extends TupleSchema ? M.$Union<FromTreeTuple<ParseTuple<S["items"], O>, S, O>> : M.$Array;
export declare type ParseTuple<S extends JSONSchema7[], O extends ParseSchemaOptions, R extends any[] = []> = {
stop: R;
continue: ParseTuple<L.Tail<S>, L.Prepend<R, ParseSchema<L.Head<S>>>>;
}[S extends [any, ...L.List] ? "continue" : "stop"];
declare type FromTreeTuple<T extends L.List, S> = ApplyAdditionalItems<ApplyBoundaries<T, "minItems" extends keyof S ? S["minItems"] : 0, "maxItems" extends keyof S ? S["maxItems"] : undefined>, "additionalItems" extends keyof S ? S["additionalItems"] : true>;
declare type ApplyBoundaries<T extends L.List, Min, Max, R = never, HasMin extends boolean = false, HasMax extends boolean = false, C = T> = {
continue: ParseTuple<L.Tail<S>, O, L.Prepend<R, ParseSchema<L.Head<S>, O>>>;
}[S extends [any, ...any[]] ? "continue" : "stop"];
declare type FromTreeTuple<T extends any[], S extends ArraySchema, O extends ParseSchemaOptions> = ApplyAdditionalItems<ApplyBoundaries<T, S extends {
minItems: number;
} ? S["minItems"] : 0, S extends {
maxItems: number;
} ? S["maxItems"] : undefined>, S extends {
additionalItems: JSONSchema7;
} ? S["additionalItems"] : true, O>;
declare type ApplyBoundaries<T extends any[], Min extends number, Max extends number | undefined, R extends any = never, HasMin extends boolean = false, HasMax extends boolean = false, C extends any[] = T> = {
stop: {
result: Max extends undefined ? R | Tuple<L.Reverse<T>, false> : HasMax extends true ? R | Tuple<L.Reverse<T>, false> : Max extends T["length"] ? Tuple<L.Reverse<T>, false> : IsLongerThan<L.Tail<T>, Max> extends true ? never : R | Tuple<L.Reverse<T>, false>;
result: Max extends undefined ? R | M.$Tuple<L.Reverse<T>> : HasMax extends true ? R | M.$Tuple<L.Reverse<T>> : Max extends T["length"] ? M.$Tuple<L.Reverse<T>> : IsLongerThan<L.Tail<T>, Max> extends true ? never : R | M.$Tuple<L.Reverse<T>>;
hasEncounteredMin: DoesExtend<Min, T["length"]>;

@@ -18,9 +36,22 @@ hasEncounteredMax: HasMax extends true ? true : Max extends T["length"] ? true : IsLongerThan<L.Tail<T>, Max>;

};
continue: ApplyBoundaries<L.Tail<T>, Min, Max, T["length"] extends Max ? Tuple<L.Reverse<T>, false> : R | Tuple<L.Reverse<T>, false>, HasMin extends true ? true : DoesExtend<Min, T["length"]>, HasMax extends true ? true : DoesExtend<Max, T["length"]>, C>;
}[Min extends T["length"] ? "stop" : T extends [any, ...L.List] ? "continue" : "stop"];
declare type IsLongerThan<T extends L.List, N, R = false> = {
continue: T["length"] extends N ? true : IsLongerThan<L.Tail<T>, N>;
continue: ApplyBoundaries<L.Tail<T>, Min, Max, T["length"] extends Max ? M.$Tuple<L.Reverse<T>> : R | M.$Tuple<L.Reverse<T>>, HasMin extends true ? true : DoesExtend<Min, T["length"]>, HasMax extends true ? true : DoesExtend<Max, T["length"]>, C>;
}[Min extends T["length"] ? "stop" : T extends [any, ...any[]] ? "continue" : "stop"];
declare type IsLongerThan<T extends any[], N extends number | undefined, R extends boolean = false> = {
continue: N extends undefined ? false : T["length"] extends N ? true : IsLongerThan<L.Tail<T>, N>;
stop: T["length"] extends N ? true : R;
}[T extends [any, ...L.List] ? "continue" : "stop"];
declare type ApplyAdditionalItems<R, A> = Get<R, "hasEncounteredMax"> extends true ? Get<R, "hasEncounteredMin"> extends true ? Get<R, "result"> : Error<'"minItems" property is lower than "maxItems"'> : A extends false ? Get<R, "hasEncounteredMin"> extends true ? Get<R, "result"> : Error<'"minItems" property is higher than allowed number of items'> : A extends true ? Get<R, "hasEncounteredMin"> extends true ? Get<R, "result"> | Tuple<L.Reverse<A.Cast<Get<R, "completeTuple">, L.List>>> : Tuple<L.Reverse<A.Cast<Get<R, "completeTuple">, L.List>>> : IsObject<A> extends true ? Get<R, "hasEncounteredMin"> extends true ? Get<R, "result"> | Tuple<L.Reverse<A.Cast<Get<R, "completeTuple">, L.List>>, true, ParseSchema<A>> : Tuple<L.Reverse<A.Cast<Get<R, "completeTuple">, L.List>>, true, ParseSchema<A>> : Error<'Invalid value in "additionalItems" property'>;
}[T extends [any, ...any[]] ? "continue" : "stop"];
declare type ApplyAdditionalItems<R extends {
result: any;
hasEncounteredMin: boolean;
hasEncounteredMax: boolean;
completeTuple: any[];
}, A extends JSONSchema7, O extends ParseSchemaOptions> = R extends {
hasEncounteredMax: true;
} ? R extends {
hasEncounteredMin: true;
} ? R["result"] : M.Never : A extends false ? R extends {
hasEncounteredMin: true;
} ? R["result"] : M.Never : A extends true ? R extends {
hasEncounteredMin: true;
} ? R["result"] | M.$Tuple<L.Reverse<R["completeTuple"]>, M.Any> : M.$Tuple<L.Reverse<R["completeTuple"]>, M.Any> : R["hasEncounteredMin"] extends true ? R["result"] | M.$Tuple<L.Reverse<R["completeTuple"]>, ParseSchema<A, O>> : M.$Tuple<L.Reverse<R["completeTuple"]>, ParseSchema<A, O>>;
export {};

@@ -1,4 +0,12 @@

import { Const, Intersection } from "../meta-types";
import { Get, HasKeyIn } from "../utils";
import { ParseSchema } from ".";
export declare type ParseConstSchema<S> = HasKeyIn<S, "type"> extends true ? Intersection<Const<Get<S, "const">>, ParseSchema<Omit<S, "const">>> : Const<Get<S, "const">>;
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { SingleTypeSchema } from "./singleType";
import { MultipleTypesSchema } from "./multipleTypes";
export declare type ConstSchema = JSONSchema7 & {
const: unknown;
};
export declare type ParseConstSchema<S extends ConstSchema, O extends ParseSchemaOptions> = S extends SingleTypeSchema ? IntersectConstAndTypeSchema<S, O> : S extends MultipleTypesSchema ? IntersectConstAndTypeSchema<S, O> : ParseConst<S>;
declare type IntersectConstAndTypeSchema<S extends ConstSchema & (SingleTypeSchema | MultipleTypesSchema), O extends ParseSchemaOptions> = M.$Intersect<ParseConst<S>, ParseSchema<Omit<S, "const">, O>>;
declare type ParseConst<S extends ConstSchema> = M.Const<S["const"]>;
export {};

@@ -1,4 +0,11 @@

import { Enum, Intersection } from "../meta-types";
import { DeepGet, HasKeyIn } from "../utils";
import { ParseSchema } from ".";
export declare type ParseEnumSchema<S> = HasKeyIn<S, "const" | "type"> extends true ? Intersection<Enum<DeepGet<S, ["enum", number]>>, ParseSchema<Omit<S, "enum">>> : Enum<DeepGet<S, ["enum", number]>>;
import { M } from "ts-algebra";
import { A } from "ts-toolbelt";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
export declare type EnumSchema = JSONSchema7 & {
enum: unknown[];
};
export declare type ParseEnumSchema<S extends EnumSchema, O extends ParseSchemaOptions> = HasKeyIn<S, "const" | "type"> extends true ? M.$Intersect<ParseEnum<S>, ParseSchema<Omit<S, "enum">, O>> : ParseEnum<S>;
declare type ParseEnum<S extends EnumSchema> = M.Enum<A.Compute<S["enum"][number]>>;
export {};

@@ -1,7 +0,15 @@

import { ParseSchema } from "../parse-schema";
import { Intersection, Union, Exclusion } from "../meta-types";
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema } from "./utils";
export declare type ParseIfThenElseSchema<S, R = Omit<S, "if" | "then" | "else">> = HasKeyIn<S, "enum" | "const" | "type" | "anyOf" | "oneOf" | "allOf" | "not"> extends true ? Intersection<ApplyIfThenElse<S, R>, ParseSchema<R>> : ApplyIfThenElse<S, R>;
declare type ApplyIfThenElse<S, R, I = "if" extends keyof S ? MergeSubSchema<R, S["if"]> : never> = Union<("then" extends keyof S ? Intersection<ParseSchema<I>, ParseSchema<MergeSubSchema<R, S["then"]>>> : ParseSchema<I>) | Exclusion<"else" extends keyof S ? ParseSchema<MergeSubSchema<R, S["else"]>> : ParseSchema<R>, ParseSchema<I>>>;
export {};
export declare type IfThenElseSchema = JSONSchema7 & {
if: JSONSchema7;
then?: JSONSchema7;
else?: JSONSchema7;
};
export declare type ParseIfThenElseSchema<S extends IfThenElseSchema, O extends ParseSchemaOptions, R extends JSONSchema7 = Omit<S, "if" | "then" | "else">, I extends JSONSchema7 = MergeSubSchema<R, S["if"]>, T extends any = S extends {
then: JSONSchema7;
} ? M.$Intersect<ParseSchema<I, O>, ParseSchema<MergeSubSchema<R, S["then"]>, O>> : ParseSchema<I, O>, E = M.$Exclude<S extends {
else: JSONSchema7;
} ? ParseSchema<MergeSubSchema<R, S["else"]>, O> : ParseSchema<R, O>, ParseSchema<I, O>>> = HasKeyIn<S, "enum" | "const" | "type" | "anyOf" | "oneOf" | "allOf" | "not"> extends true ? M.$Intersect<M.$Union<T | E>, ParseSchema<R, O>> : M.$Union<T | E>;

@@ -1,31 +0,17 @@

import { Primitive, Any, Never } from "../meta-types";
import { ParseConstSchema } from "./const";
import { ParseEnumSchema } from "./enum";
import { ParseMixedSchema } from "./mixed";
import { ParseArrSchema } from "./array";
import { ParseObjectSchema } from "./object";
import { ParseAnyOfSchema } from "./anyOf";
import { ParseOneOfSchema } from "./oneOf";
import { ParseAllOfSchema } from "./allOf";
import { ParseNotSchema } from "./not";
import { ParseIfThenElseSchema } from "./ifThenElse";
export declare type ParseSchema<S> = {
any: Any;
never: Never;
null: Primitive<null>;
boolean: Primitive<boolean>;
number: Primitive<number>;
string: Primitive<string>;
mixed: ParseMixedSchema<S>;
object: ParseObjectSchema<S>;
array: ParseArrSchema<S>;
const: ParseConstSchema<S>;
enum: ParseEnumSchema<S>;
anyOf: ParseAnyOfSchema<S>;
oneOf: ParseOneOfSchema<S>;
allOf: ParseAllOfSchema<S>;
not: ParseNotSchema<S>;
ifThenElse: ParseIfThenElseSchema<S>;
}[InferSchemaType<S>];
declare type InferSchemaType<S> = S extends true | string ? "any" : S extends false ? "never" : "if" extends keyof S ? "ifThenElse" : "not" extends keyof S ? "not" : "allOf" extends keyof S ? "allOf" : "oneOf" extends keyof S ? "oneOf" : "anyOf" extends keyof S ? "anyOf" : "enum" extends keyof S ? "enum" : "const" extends keyof S ? "const" : "type" extends keyof S ? S["type"] extends any[] ? "mixed" : S["type"] extends "null" ? "null" : S["type"] extends "boolean" ? "boolean" : S["type"] extends "integer" | "number" ? "number" : S["type"] extends "string" ? "string" : S["type"] extends "object" ? "object" : S["type"] extends "array" ? "array" : "never" : "any";
export {};
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { And, DoesExtend } from "../utils";
import { ConstSchema, ParseConstSchema } from "./const";
import { EnumSchema, ParseEnumSchema } from "./enum";
import { ParseSingleTypeSchema, SingleTypeSchema } from "./singleType";
import { MultipleTypesSchema, ParseMultipleTypesSchema } from "./multipleTypes";
import { AnyOfSchema, ParseAnyOfSchema } from "./anyOf";
import { OneOfSchema, ParseOneOfSchema } from "./oneOf";
import { AllOfSchema, ParseAllOfSchema } from "./allOf";
import { ParseNotSchema, NotSchema } from "./not";
import { ParseIfThenElseSchema, IfThenElseSchema } from "./ifThenElse";
export declare type ParseSchemaOptions = {
parseNotKeyword: boolean;
parseIfThenElseKeywords: boolean;
};
export declare type ParseSchema<S extends JSONSchema7, O extends ParseSchemaOptions> = JSONSchema7 extends S ? M.Any : S extends true | string ? M.Any : S extends false ? M.Never : And<DoesExtend<O["parseIfThenElseKeywords"], true>, DoesExtend<S, IfThenElseSchema>> extends true ? S extends IfThenElseSchema ? ParseIfThenElseSchema<S, O> : never : And<DoesExtend<O["parseNotKeyword"], true>, DoesExtend<S, NotSchema>> extends true ? S extends NotSchema ? ParseNotSchema<S, O> : never : S extends AllOfSchema ? ParseAllOfSchema<S, O> : S extends OneOfSchema ? ParseOneOfSchema<S, O> : S extends AnyOfSchema ? ParseAnyOfSchema<S, O> : S extends EnumSchema ? ParseEnumSchema<S, O> : S extends ConstSchema ? ParseConstSchema<S, O> : S extends MultipleTypesSchema ? ParseMultipleTypesSchema<S, O> : S extends SingleTypeSchema ? ParseSingleTypeSchema<S, O> : M.Any;

@@ -1,8 +0,11 @@

import { Any, Primitive, Arr, Object, Union, Exclusion } from "../meta-types";
import { IsRepresentable } from "../meta-types/utils";
import { Get, HasKeyIn } from "../utils";
import { ParseSchema } from ".";
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema } from "./utils";
declare type AllTypes = Union<Primitive<null> | Primitive<boolean> | Primitive<number> | Primitive<string> | Arr<Any> | Object>;
export declare type ParseNotSchema<S, P = ParseSchema<Omit<S, "not">>, E = Exclusion<HasKeyIn<S, "enum" | "const" | "type" | "anyOf" | "oneOf" | "allOf"> extends true ? P : AllTypes, ParseSchema<MergeSubSchema<Omit<S, "not">, Get<S, "not">>>>> = IsRepresentable<E> extends true ? E : P;
export declare type NotSchema = JSONSchema7 & {
not: JSONSchema7;
};
declare type AllTypes = M.Union<M.Primitive<null> | M.Primitive<boolean> | M.Primitive<number> | M.Primitive<string> | M.Array<M.Any> | M.Object<{}, never, M.Any>>;
export declare type ParseNotSchema<S extends NotSchema, O extends ParseSchemaOptions, P extends any = ParseSchema<Omit<S, "not">, O>, E = M.$Exclude<HasKeyIn<S, "enum" | "const" | "type" | "anyOf" | "oneOf" | "allOf"> extends true ? P : AllTypes, ParseSchema<MergeSubSchema<Omit<S, "not">, S["not"]>, O>>> = E extends M.Never ? P : E;
export {};

@@ -1,23 +0,28 @@

import { Object, Any, Never, Union, Error } from "../meta-types";
import { IsObject } from "../utils";
import { ParseSchema } from ".";
export declare type ParseObjectSchema<S> = "properties" extends keyof S ? Object<{
[key in keyof S["properties"]]: ParseSchema<S["properties"][key]>;
}, GetRequired<S>, "additionalProperties" extends keyof S ? S["additionalProperties"] extends false ? false : true : true, GetOpenProps<S>> : Object<{}, GetRequired<S>, true, GetOpenProps<S>>;
declare type GetRequired<S> = S extends {
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { ParseSchema, ParseSchemaOptions } from "./index";
export declare type ObjectSchema = JSONSchema7 & {
type: "object";
};
export declare type ParseObjectSchema<S extends ObjectSchema, O extends ParseSchemaOptions> = S extends {
properties: Record<string, JSONSchema7>;
} ? M.$Object<{
[key in keyof S["properties"]]: ParseSchema<S["properties"][key], O>;
}, GetRequired<S>, GetOpenProps<S, O>> : M.$Object<{}, GetRequired<S>, GetOpenProps<S, O>>;
declare type GetRequired<S extends ObjectSchema> = S extends {
required: ReadonlyArray<string>;
} ? S["required"][number] : never;
declare type GetOpenProps<S> = "additionalProperties" extends keyof S ? "patternProperties" extends keyof S ? AdditionalAndPatternProps<S["additionalProperties"], S["patternProperties"]> : AdditionalProps<S["additionalProperties"]> : "patternProperties" extends keyof S ? PatternProps<S["patternProperties"]> : Any;
declare type AdditionalProps<A> = A extends false ? Never : A extends true ? Any : IsObject<A> extends true ? ParseSchema<A> : Error<'Invalid value in "additionalProperties" property'>;
declare type PatternProps<P> = {
type: "union";
values: {
[key in keyof P]: ParseSchema<P[key]>;
}[keyof P];
};
declare type AdditionalAndPatternProps<A, P> = A extends boolean ? Union<{
[key in keyof P]: ParseSchema<P[key]>;
}[keyof P]> : IsObject<A> extends true ? Union<ParseSchema<A> | {
[key in keyof P]: ParseSchema<P[key]>;
}[keyof P]> : never;
declare type GetOpenProps<S extends ObjectSchema, O extends ParseSchemaOptions> = S extends {
additionalProperties: JSONSchema7;
} ? S extends {
patternProperties: Record<string, JSONSchema7>;
} ? AdditionalAndPatternProps<S["additionalProperties"], S["patternProperties"], O> : ParseSchema<S["additionalProperties"], O> : S extends {
patternProperties: Record<string, JSONSchema7>;
} ? PatternProps<S["patternProperties"], O> : M.Any;
declare type PatternProps<P extends Record<string, JSONSchema7>, O extends ParseSchemaOptions> = M.$Union<{
[key in keyof P]: ParseSchema<P[key], O>;
}[keyof P]>;
declare type AdditionalAndPatternProps<A extends JSONSchema7, P extends Record<string, JSONSchema7>, O extends ParseSchemaOptions> = A extends boolean ? PatternProps<P, O> : M.$Union<ParseSchema<A, O> | {
[key in keyof P]: ParseSchema<P[key], O>;
}[keyof P]>;
export {};

@@ -0,11 +1,15 @@

import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { Intersection, Union, Error } from "../meta-types";
import { Get, HasKeyIn, Merge } from "../utils";
import { ParseSchema } from ".";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn, Merge } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema, RemoveInvalidAdditionalItems } from "./utils";
export declare type ParseOneOfSchema<S, O = Get<S, "oneOf">> = O extends L.List ? Union<RecurseOnOneOfSchema<O, S>> : Error<"'oneOf' property should be an array">;
declare type RecurseOnOneOfSchema<S extends L.List, P, R = never> = {
export declare type OneOfSchema = JSONSchema7 & {
oneOf: JSONSchema7[];
};
export declare type ParseOneOfSchema<P extends OneOfSchema, O extends ParseSchemaOptions> = M.$Union<RecurseOnOneOfSchema<P["oneOf"], P, O>>;
declare type RecurseOnOneOfSchema<S extends JSONSchema7[], P extends OneOfSchema, O extends ParseSchemaOptions, R extends any = never> = {
stop: R;
continue: RecurseOnOneOfSchema<L.Tail<S>, P, R | (HasKeyIn<P, "enum" | "const" | "type" | "anyOf"> extends true ? Intersection<ParseSchema<Omit<P, "oneOf">>, ParseSchema<MergeSubSchema<Omit<P, "oneOf">, L.Head<S>>>> : ParseSchema<Merge<Omit<P, "oneOf">, RemoveInvalidAdditionalItems<L.Head<S>>>>)>;
}[S extends [any, ...L.List] ? "continue" : "stop"];
continue: RecurseOnOneOfSchema<L.Tail<S>, P, O, R | (HasKeyIn<P, "enum" | "const" | "type" | "anyOf"> extends true ? M.$Intersect<ParseSchema<Omit<P, "oneOf">, O>, ParseSchema<MergeSubSchema<Omit<P, "oneOf">, L.Head<S>>, O>> : ParseSchema<Merge<Omit<P, "oneOf">, RemoveInvalidAdditionalItems<L.Head<S>>>, O>)>;
}[S extends [any, ...any[]] ? "continue" : "stop"];
export {};

@@ -1,9 +0,15 @@

import { Merge } from "../utils";
export declare type RemoveInvalidAdditionalItems<S> = "items" extends keyof S ? "additionalItems" extends keyof S ? S : Merge<S, {
import { JSONSchema7 } from "../definitions";
export declare type RemoveInvalidAdditionalItems<S extends JSONSchema7> = S extends {
items: JSONSchema7 | JSONSchema7[];
} ? S extends {
additionalItems: JSONSchema7;
} ? S : S & {
additionalItems: true;
}> : Omit<S, "additionalItems">;
export declare type MergeSubSchema<P, C> = Merge<P, Merge<{
} : S extends boolean ? S : Omit<S, "additionalItems">;
declare type EmptySchema = {
properties: {};
additionalProperties: true;
required: [];
}, RemoveInvalidAdditionalItems<C>>>;
};
export declare type MergeSubSchema<P extends JSONSchema7, S extends JSONSchema7, R extends JSONSchema7 = RemoveInvalidAdditionalItems<S>, C extends JSONSchema7 = Omit<EmptySchema, keyof R> & R> = Omit<P, keyof C> & C;
export {};

@@ -17,9 +17,2 @@ /**

export declare type IsObject<T> = T extends object ? ArrayKeys extends Extract<keyof T, ArrayKeys> ? false : true : false;
/**
* Returns `true` if type is array, `false` if not (excludes objects)
*
* @param T Type
* @return Boolean
*/
export declare type IsArray<T> = T extends object ? ArrayKeys extends Extract<keyof T, ArrayKeys> ? true : false : false;
export {};
export { And } from "./and";
export { DoesExtend, IsObject, IsArray } from "./extends";
export { Get, DeepGet } from "./get";
export { DoesExtend, IsObject } from "./extends";
export { HasKeyIn } from "./hasKeyIn";
export { DeepMergeSafe, DeepMergeUnsafe, Merge } from "./merge";
export { Not } from "./not";
export { Or } from "./or";
export { Prettify } from "./prettify";
export { Merge } from "./merge";
export { Readonly } from "./readonly";
export { Writable } from "./writable";

@@ -1,34 +0,3 @@

import { A, L } from "ts-toolbelt";
import { IsObject, IsArray } from "./extends";
import { IsObject } from "./extends";
/**
* Recursively merge two types `A` and `B`:
* - Returns `B` if `A` and `B` are not both objects or arrays
* - Recursively merge `A` and `B` properties if both are objects
* - Concat `A` and `B` if both are arrays
*
* `DeepMergeUnsafe` preserves non-required properties, but can return `never` if TS infers that `A & B = never` (which can happen if some properties are incompatible)
*
* @param A Type
* @param B Type
* @return Type
*/
export declare type DeepMergeUnsafe<A, B> = IsObject<A> extends true ? IsObject<B> extends true ? {
[K in keyof (A & B)]: K extends keyof B ? K extends keyof A ? DeepMergeUnsafe<A[K], B[K]> : B[K] : K extends keyof A ? A[K] : never;
} : B : IsArray<A> extends true ? IsArray<B> extends true ? B extends L.List ? L.Concat<A.Cast<A, L.List>, B> : never : B : B;
/**
* Recursively merge two types `A` and `B`:
* - Returns `B` if `A` and `B` are not both objects or arrays
* - Recursively merge `A` and `B` properties if both are objects
* - Concat `A` and `B` if both are arrays
*
* Contrary to `DeepMergeUnsafe`, `DeepMergeSafe` never returns `never`, but doesn't preserve non-required properties
*
* @param A Type
* @param B Type
* @return Type
*/
export declare type DeepMergeSafe<A, B> = IsObject<A> extends true ? IsObject<B> extends true ? {
[K in keyof A | keyof B]: K extends keyof B ? K extends keyof A ? DeepMergeSafe<A[K], B[K]> : B[K] : K extends keyof A ? A[K] : never;
} : B : IsArray<A> extends true ? IsArray<B> extends true ? B extends any[] ? L.Concat<A.Cast<A, L.List>, B> : never : B : B;
/**
* Merge two types `A` and `B`:

@@ -35,0 +4,0 @@ * - Returns `B` if `A` and `B` are not both objects

{
"name": "json-schema-to-ts",
"version": "1.6.5",
"version": "2.0.0-beta.0",
"description": "Infer typescript types from your JSON schemas!",

@@ -8,20 +8,22 @@ "main": "lib/index.d.ts",

"release": "bash scripts/release.bash",
"test": "tsc --noEmit && jest --verbose"
"test": "tsc --noEmit && jest --verbose",
"type": "tsc --noEmit"
},
"dependencies": {
"@types/json-schema": "^7.0.6",
"ts-toolbelt": "^6.15.5"
"@types/json-schema": "^7.0.9",
"ts-toolbelt": "^9.6.0",
"ts-algebra": "^1.0.1"
},
"devDependencies": {
"@babel/core": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"@babel/preset-typescript": "^7.10.4",
"@types/jest": "^26.0.4",
"ajv": "^6.12.6",
"babel-jest": "^26.1.0",
"jest": "^26.6.3",
"rollup": "^2.45.2",
"rollup-plugin-dts": "1.4.10",
"@babel/core": "^7.17.5",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@types/jest": "^27.4.0",
"ajv": "^8.10.0",
"babel-jest": "^27.5.1",
"jest": "^27.1.1",
"rollup": "^2.67.3",
"rollup-plugin-dts": "4.1.0",
"rollup-plugin-import-map": "^2.2.2",
"typescript": "^3.9.6"
"typescript": "^4.5.5"
},

@@ -28,0 +30,0 @@ "author": "Thomas Aribart",

@@ -418,4 +418,2 @@ <img src="assets/header-round-medium.png" width="100%" align="center" />

> This may be revised soon now that `not` exclusions are now possible
### AllOf

@@ -455,2 +453,4 @@

Exclusions require heavy computations, that can sometimes be aborted by Typescript and end up in `any` inferred types. For this reason, they are not activated by default: You can opt-in with the `parseNotKeyword` option.
```typescript

@@ -466,3 +466,3 @@ const tupleSchema = {

type Tuple = FromSchema<typeof tupleSchema>;
type Tuple = FromSchema<typeof tupleSchema, { parseNotKeyword: true }>;
// => [] | [1, 2]

@@ -478,3 +478,6 @@ ```

type PrimitiveType = FromSchema<typeof primitiveTypeSchema>;
type PrimitiveType = FromSchema<
typeof primitiveTypeSchema,
{ parseNotKeyword: true }
>;
// => null | boolean | number | string

@@ -499,3 +502,3 @@ ```

type Pet = FromSchema<typeof petSchema>;
type Pet = FromSchema<typeof petSchema, { parseNotKeyword: true }>;
// => { animal: "cat" | "dog" }

@@ -519,3 +522,3 @@ ```

type Pet = FromSchema<typeof petSchema>;
type Pet = FromSchema<typeof petSchema, { parseNotKeyword: true }>;
// => { animal: "cat" | "dog", color: "black" | "brown" | "white" }

@@ -532,3 +535,3 @@ ```

type OddNumber = FromSchema<typeof oddNumberSchema>;
type OddNumber = FromSchema<typeof oddNumberSchema, { parseNotKeyword: true }>;
// => should and will resolve to "number"

@@ -541,3 +544,3 @@

type Incorrect = FromSchema<typeof incorrectSchema>;
type Incorrect = FromSchema<typeof incorrectSchema, { parseNotKeyword: true }>;
// => should resolve to "never" but will still resolve to "number"

@@ -556,3 +559,6 @@ ```

type GoodLanguage = FromSchema<typeof goodLanguageSchema>;
type GoodLanguage = FromSchema<
typeof goodLanguageSchema,
{ parseNotKeyword: true }
>;
// => string

@@ -563,2 +569,4 @@ ```

For the same reason as the `Not` keyword, conditions parsing is not activated by default: You can opt-in with the `parseIfThenElseKeywords` option.
```typescript

@@ -589,5 +597,12 @@ const petSchema = {

type Pet = FromSchema<typeof petSchema>;
// => { animal: "dog"; dogBreed: DogBreed }
// | { animal: "cat"; catBreed: CatBreed }
type Pet = FromSchema<typeof petSchema, { parseIfThenElseKeywords: true }>;
// => {
// animal: "dog";
// dogBreed: DogBreed;
// catBreed?: CatBreed | undefined
// } | {
// animal: "cat" | "dog";
// catBreed: CatBreed;
// dogBreed?: DogBreed | undefined
// }
```

@@ -605,105 +620,5 @@

### Does `json-schema-to-ts` work on _.json_ file schemas ?
Sadly, no 😭
`FromSchema` is based on type computations. By design, it only works on "good enough" material, i.e. _narrow_ types (`{ type: "string" }`) and NOT _widened_ ones (`{ type: string }` which can also represent `{ type: "number" }`). However, JSON imports are **widened by default**. This is native TS behavior, there's no changing that.
If you really want use _.json_ files, you can start by [upvoting this feature request to implement _.json_ imports `as const`](https://github.com/microsoft/TypeScript/issues/32063) on the official repo 🙂 AND you can always cast imported schemas as their narrow types:
```json
// dog.json
{
"type": "object",
"properties": {
"name": { "type": "string" },
"age": { "type": "integer" },
"hobbies": { "type": "array", "items": { "type": "string" } },
"favoriteFood": { "enum": ["pizza", "taco", "fries"] }
},
"required": ["name", "age"]
}
```
```typescript
import { FromSchema } from "json-schema-to-ts";
import dogRawSchema from "./dog.json";
const dogSchema = dogRawSchema as {
type: "object";
properties: {
name: { type: "string" };
age: { type: "integer" };
hobbies: { type: "array"; items: { type: "string" } };
favoriteFood: { enum: ["pizza", "taco", "fries"] };
};
required: ["name", "age"];
};
type Dog = FromSchema<typeof dogSchema>;
// => Will work 🙌
```
It is technically code duplication, BUT TS will throw an errow if the narrow and widened types don't sufficiently overlap, which allows for partial type safety (roughly, everything but the object "leafs"). In particular, this will work well on object properties names, as object keys are not widened by default.
```typescript
import { FromSchema } from "json-schema-to-ts";
import dogRawSchema from "./dog.json";
const dogSchema = dogoRawSchema as {
type: "object";
properties: {
name: { type: "number" }; // "number" instead of "string" will go undetected...
years: { type: "integer" }; // ...but "years" instead of "age" will not 🙌
hobbies: { type: "array"; items: { type: "string" } };
favoriteFood: { const: "pizza" }; // ..."const" instead of "enum" as well 🙌
};
required: ["name", "age"];
};
```
### Can I assign `JSONSchema` to my schema and use `FromSchema` at the same time ?
`json-schema-to-ts` exports a `JSONSchema` type to help you write schemas:
```typescript
import { FromSchema, JSONSchema } from "json-schema-to-ts";
const dogSchema: JSONSchema = { ... } as const;
type Dog = FromSchema<typeof dogSchema>;
```
For the same reason, this example will **not** work 🙅‍♂️
`FromSchema` is based on type computations. By design, it only works on "good enough" material, i.e. _narrow_ types (`{ type: "string" }`) and NOT _widened_ ones (`{ type: string }` which can also represent `{ type: "number" }`). For the compiler, assigning `JSONSchema` to your schema "blurs" its type to pretty much the **widest possible schema type** 😅 That's why in this example, `Dog` will be equal to the `never` type.
So there is a sort of Heiseinberg's uncertainty principle at play here: You can either use `FromSchema` or `JSONSchema`, but not both at the same time.
The correct way to use them is the following:
- Define a schema with the `as const` statement
- Assign the `JSONSchema` type to it (allowing autocompletion and precise error highlighting)
- Write the schema
- Remove the type assignment
- Use `FromSchema` 🙌
### Will `json-schema-to-ts` impact the performances of my IDE/compiler ?
Long story short: no.
In your IDE, as long as you don't define all your schemas in the same file (which you shouldn't do anyway), file opening and type infering is still fast enough for you not to hardly notice anything, even on large schemas (200+ lines).
The same holds true for compilation. As far as I know (please, feel free to open an issue if you find otherwise), `json-schema-to-ts` has little to no impact on the TS compilation time.
### I get a `type instantiation is excessively deep and potentially infinite` error, what should I do ?
Since Typescript 4.0 (which was unfortunately released after `json-schema-to-ts` 😅), the TS compiler raises this error when detecting long type computations, and potential infinite loops.
`FromSchema` goes through some pretty wild type recursions, so this is can be an issue on large schemas, particularly when using intersections (`allOf`) and exclusions (`not`, `else`).
I am working on simplifying the type computations. But for the moment, I don't have any better solution to give you other than ignoring the error with a `@ts-ignore` comment. This should not block the type computation, so the inferred type should still be valid.
If you find that it's not the case, please, feel free to open an issue.
- [Does `json-schema-to-ts` work on _.json_ file schemas?](./documentation/FAQs/does-json-schema-to-ts-work-on-json-file-schemas.md)
- [Can I assign `JSONSchema` to my schema and use `FromSchema` at the same time?](./documentation/FAQs/can-i-assign-jsonschema-to-my-schema-and-use-fromschema-at-the-same-time.md)
- [Will `json-schema-to-ts` impact the performances of my IDE/compiler?](./documentation/FAQs/will-json-schema-to-ts-impact-the-performances-of-my-ide-compiler.md)
- [I get a `type instantiation is excessively deep and potentially infinite` error, what should I do?](./documentation/FAQs/i-get-a-type-instantiation-is-excessively-deep-and-potentially-infinite-error-what-should-i-do.md)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc