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 2.4.0 to 2.5.0

lib/type-guards/compiler.d.ts

4

lib/index.d.ts

@@ -7,3 +7,5 @@ import { M } from "ts-algebra";

import type { Readonly, Writable } from "./utils";
export { DeserializationPattern, FromSchemaOptions, FromSchemaDefaultOptions, } from "./definitions";
export type { DeserializationPattern, FromSchemaOptions, FromSchemaDefaultOptions, } from "./definitions";
export type { Compiler, Validator } from "./type-guards";
export { wrapCompilerAsTypeGuard, wrapValidatorAsTypeGuard, } from "./type-guards";
export declare type JSONSchema7 = $JSONSchema7 | Readonly<$JSONSchema7>;

@@ -10,0 +12,0 @@ export declare type JSONSchema7Reference = $JSONSchema7Reference | Readonly<$JSONSchema7Reference>;

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

export {};
export { wrapCompilerAsTypeGuard, wrapValidatorAsTypeGuard, } from "./type-guards";
import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";

@@ -10,3 +9,3 @@ import { MergeSubSchema } from "./utils";

};
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>;
export declare type ParseAllOfSchema<P extends AllOfSchema, O extends ParseSchemaOptions> = RecurseOnAllOfSchema<P["allOf"], P, O, ParseSchema<Omit<P, "allOf">, O>>;
declare type RecurseOnAllOfSchema<S extends JSONSchema7[], P extends AllOfSchema, O extends ParseSchemaOptions, R> = {

@@ -13,0 +12,0 @@ stop: R;

import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn, Merge } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema, RemoveInvalidAdditionalItems } from "./utils";
import { MergeSubSchema } from "./utils";
export declare type AnyOfSchema = JSONSchema7 & {

@@ -13,4 +12,4 @@ anyOf: JSONSchema7[];

stop: R;
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>)>;
continue: RecurseOnAnyOfSchema<L.Tail<S>, P, O, R | M.$Intersect<ParseSchema<Omit<P, "anyOf">, O>, ParseSchema<MergeSubSchema<Omit<P, "anyOf">, L.Head<S>>, O>>>;
}[S extends [any, ...any[]] ? "continue" : "stop"];
export {};
import { M } from "ts-algebra";
import { A } from "ts-toolbelt";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";

@@ -9,4 +8,4 @@ export declare type EnumSchema = JSONSchema7 & {

};
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>;
export declare type ParseEnumSchema<S extends EnumSchema, O extends ParseSchemaOptions> = M.$Intersect<ParseEnum<S>, ParseSchema<Omit<S, "enum">, O>>;
declare type ParseEnum<S extends EnumSchema> = M.Enum<A.Compute<S["enum"][number]>>;
export {};
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";

@@ -15,2 +14,2 @@ import { MergeSubSchema } from "./utils";

else: JSONSchema7;
} ? M.$Intersect<M.$Exclude<ParseSchema<R, O>, ParseSchema<I, O>>, ParseSchema<MergeSubSchema<R, S["else"]>, O>> : M.$Exclude<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>;
} ? M.$Intersect<M.$Exclude<ParseSchema<R, O>, ParseSchema<I, O>>, ParseSchema<MergeSubSchema<R, S["else"]>, O>> : M.$Exclude<ParseSchema<R, O>, ParseSchema<I, O>>> = M.$Intersect<M.$Union<T | E>, ParseSchema<R, O>>;
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";

@@ -10,3 +9,3 @@ import { MergeSubSchema } from "./utils";

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 = 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 declare type ParseNotSchema<S extends NotSchema, O extends ParseSchemaOptions, P = ParseSchema<Omit<S, "not">, O>, E = M.$Exclude<P extends M.AnyType ? M.$Intersect<AllTypes, P> : P, ParseSchema<MergeSubSchema<Omit<S, "not">, S["not"]>, O>>> = E extends M.Never ? P : E;
export {};
import { M } from "ts-algebra";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn } from "../utils";
export declare type NullableSchema = JSONSchema7 & {

@@ -8,4 +7,4 @@ nullable: boolean;

import { ParseSchema, ParseSchemaOptions } from "./index";
export declare type ParseNullableSchema<S extends NullableSchema, O extends ParseSchemaOptions, R = HasKeyIn<S, "enum" | "const" | "type" | "anyOf" | "oneOf" | "allOf" | "not" | "if"> extends true ? ParseSchema<Omit<S, "nullable">, O> : M.Any> = S extends {
export declare type ParseNullableSchema<S extends NullableSchema, O extends ParseSchemaOptions, R = ParseSchema<Omit<S, "nullable">, O>> = S extends {
nullable: true;
} ? M.$Union<M.Primitive<null> | R> : R;
import { M } from "ts-algebra";
import { L } from "ts-toolbelt";
import { JSONSchema7 } from "../definitions";
import { HasKeyIn, Merge } from "../utils";
import { ParseSchema, ParseSchemaOptions } from "./index";
import { MergeSubSchema, RemoveInvalidAdditionalItems } from "./utils";
import { MergeSubSchema } from "./utils";
export declare type OneOfSchema = JSONSchema7 & {

@@ -13,4 +12,4 @@ oneOf: JSONSchema7[];

stop: R;
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>)>;
continue: RecurseOnOneOfSchema<L.Tail<S>, P, O, R | M.$Intersect<ParseSchema<Omit<P, "oneOf">, O>, ParseSchema<MergeSubSchema<Omit<P, "oneOf">, L.Head<S>>, O>>>;
}[S extends [any, ...any[]] ? "continue" : "stop"];
export {};
export { And } from "./and";
export { DoesExtend, IsObject } from "./extends";
export { DeepGet } from "./get";
export { HasKeyIn } from "./hasKeyIn";
export { Merge } from "./merge";
export { DeepReadonly as Readonly } from "./readonly";
export { DeepWritable as Writable } from "./writable";
{
"name": "json-schema-to-ts",
"version": "2.4.0",
"version": "2.5.0",
"description": "Infer typescript types from your JSON schemas!",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

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

- [Deserialization](#deserialization)
- [Typeguards](#typeguards)
- [Validators](#validators)
- [Compilers](#compilers)
- [FAQ](#frequently-asked-questions)

@@ -645,3 +648,3 @@

Unlink run-time validator classes like [AJV](https://github.com/ajv-validator/ajv), TS types cannot withhold internal states. Thus, they cannot keep any identified schemas in memory.
Unlike run-time validator classes like [AJV](https://github.com/ajv-validator/ajv), TS types cannot withhold internal states. Thus, they cannot keep any identified schemas in memory.

@@ -738,2 +741,85 @@ But you can hydrate them via the `references` option:

## Typeguards
You can use `FromSchema` to implement your own typeguard:
```typescript
import { FromSchema } from "json-schema-to-ts";
const validate = <S extends JSONSchema, T = FromSchema<S>>(
schema: S,
data: unknown
): data is T => {
const isDataValid: boolean = ... // Implement validation here
return isDataValid
}
const petSchema = { ... } as const
let data:unknown
if (validate(petSchema, data)) {
const { name, ... } = data; // <= data is typed as Pet 🙌
}
```
But `json-schema-to-ts` also exposes two helpers to write type guards. They don't impact the code that you wrote (they simply `return` it), but turn it into type guards.
You can use them to wrap either [`validators`](#validator) or [`compilers`](#compiler).
### Validators
A validator is a function that receives a schema plus some data, and returns `true` if the data is valid compared to the schema, `false` otherwise.
You can use the `wrapValidatorAsTypeGuard` helper to turn validators into type guards. Here is an implementation with [ajv](https://ajv.js.org/):
```typescript
import Ajv from "ajv";
import { Validator, wrapValidatorAsTypeGuard } from "json-schema-to-ts";
const ajv = new Ajv();
// The validator definition is up to you
const $validate: Validator = (schema, data)
=> ajv.validate(schema, data);
const validate = wrapValidatorAsTypeGuard<{
// You can provide FromSchema options as generic type
parseNotKeyword: true;
}>($validate);
const petSchema = { ... } as const;
let data: unknown;
if (validate(petSchema, data)) {
const { name, ... } = data; // <= data is typed as Pet 🙌
}
```
### Compilers
A compiler is a function that takes a schema as an input and returns a data validator for this schema as an output.
You can use the `wrapCompilerAsTypeGuard` helper to turn compilers into type guard builders. Here is an implementation with [ajv](https://ajv.js.org/):
```typescript
import Ajv from "ajv";
import { Compiler, wrapCompilerAsTypeGuard } from "json-schema-to-ts";
// The compiler definition is up to you
const $compile: Compiler = (schema) => ajv.compile(schema);
const compile = wrapCompilerAsTypeGuard<{
// You can provide FromSchema options as generic type
parseNotKeyword: true;
}>($compile);
const petSchema = { ... } as const;
const isPet = compile(petSchema);
let data: unknown;
if (isPet(data)) {
const { name, ... } = data; // <= data is typed as Pet 🙌
}
```
## Frequently Asked Questions

@@ -740,0 +826,0 @@

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