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.0.0 to 1.1.0

src/parse-schema/oneOf.ts

2

package.json
{
"name": "json-schema-to-ts",
"version": "1.0.0",
"version": "1.1.0",
"description": "Infer typescript types from your JSON schemas!",

@@ -5,0 +5,0 @@ "main": "src/index.ts",

@@ -33,3 +33,3 @@ # Stop typing twice 🙅‍♂️

The `FromSchema` method allows infering TS types directly from JSON schemas:
The `FromSchema` method lets you infer TS types directly from JSON schemas:

@@ -75,4 +75,55 @@ ```typescript

**Note**: The `as const` statement is used so that TypeScript takes the schema definition to the word (e.g. _true_ is interpreted as the _true_ constant and not widened as _boolean_). It is pure TypeScript and has zero impact on the compiled code.
> The `as const` statement is used so that TypeScript takes the schema definition to the word (e.g. _true_ is interpreted as the _true_ constant and not widened as _boolean_). It is pure TypeScript and has zero impact on the compiled code.
# Why use json-schema-to-ts ?
There are many librearies out there like [zod]() (type first validation) our . Feel free to check them, they may suit your needs better!
However, if you prefer to stick to JSON schemas (and there are good reasons to do so) AND if you can define your schemas in TS (the `as const` statement doesn't work on JSON imports... for now !), then `json-schema-to-ts` is made for you !
Doesn't impact, what's lighter than a dev-only library ?
Let's face it, making small errors in JSON schemas is easy!
Which has the benefits:
- 0 impact on compiled code: What's lighter than a dev dependancy ?
- 📝 Reduce redundancy: Writing less code is always nice!
- 🤝 Enforce consistency: Spot typos, or a string which should be an enum...
- ⚡️ Be right first-time: With instantaneous feedback given by TS !
- Types are easier to read than shemas. Help you write better schemas
For instance, is it obvious that the following schema can never be validated ?
```typescript
const addressSchema = {
type: "object",
allOf: [
{
properties: {
street: { type: "string" },
city: { type: "string" },
state: { type: "string" },
},
required: ["street", "city", "state"],
},
{
properties: {
type: { enum: ["residential", "business"] },
},
},
],
additionalProperties: false,
} as const;
```
Well, it is with `FromSchema` !
```typescript
type Address = FromSchema<typeof addressSchema>;
// => never 🙌
```
Extensively tested against the [ajv library](https://github.com/ajv-validator/ajv). Trust!
# Docs

@@ -215,3 +266,3 @@

(NOTE: Additional items will only work if Typescript's `strictNullChecks` option is activated)
> Additional items will only work if Typescript's `strictNullChecks` option is activated

@@ -323,4 +374,26 @@ ### Objects

...Coming soon 😃
Because TypeScript doesn't have [refinment types](https://en.wikipedia.org/wiki/Refinement_type), `oneOf` will behave strictly the same as `anyOf`.
```typescript
const catSchema = {
type: "object",
oneOf: [
{ properties: { name: { type: "string" } }, required: ["name"] },
{ properties: { color: { enum: ["black", "brown", "white"] } } },
],
} as const;
type Cat = FromSchema<typeof catSchema>;
// => {
// [x: string]: unknown;
// name: string;
// } | {
// [x: string]: unknown;
// color?: "black" | "brown" | "white";
// }
// => FromSchema cannot detect the following invalid obj 😱
const invalidCat: Cat = { name: "Garfield" };
```
### AllOf

@@ -327,0 +400,0 @@

import { Litteral, Any, Never } from "../meta-types";
import { HasKeyIn } from "../utils";
import { ParseAnyOfSchema } from "./anyOf";
import { ParseConstSchema } from "./const";
import { ParseEnumSchema } from "./enum";
import { ParseConstSchema } from "./const";
import { ParseMixedSchema } from "./mixed";
import { ParseArrSchema } from "./array";
import { ParseObjectSchema } from "./object";
import { ParseArrSchema } from "./array";
import { ParseAnyOfSchema } from "./anyOf";
import { ParseOneOfSchema } from "./oneOf";

@@ -24,2 +24,3 @@ export type ParseSchema<S> = {

anyOf: ParseAnyOfSchema<S>;
oneOf: ParseOneOfSchema<S>;
}[InferSchemaType<S>];

@@ -31,7 +32,9 @@

? "never"
: HasKeyIn<S, "anyOf"> extends true
: "oneOf" extends keyof S
? "oneOf"
: "anyOf" extends keyof S
? "anyOf"
: HasKeyIn<S, "enum"> extends true
: "enum" extends keyof S
? "enum"
: HasKeyIn<S, "const"> extends true
: "const" extends keyof S
? "const"

@@ -38,0 +41,0 @@ : "type" extends keyof S

@@ -5,3 +5,3 @@ import { Head } from "./head";

/**
* Returns the property value of an object `O`, `F` if property key misses from object
* Returns the value at key `K` in object `O`, `F` if `K` misses from object
*

@@ -15,5 +15,13 @@ * @param O Object

export type GetRec<O, K> = {
continue: GetRec<Get<O, Head<K>>, Tail<K>>;
/**
* Returns the value at path `P` in object `O`, `F` if `P` misses from object
*
* @param O Object
* @param P Path
* @param F _(optional:_ `never` _)_ Fallback type
* @return Type
*/
export type GetRec<O, P, F = never> = {
continue: Head<P> extends keyof O ? GetRec<O[Head<P>], Tail<P>, F> : F;
stop: O;
}[K extends [any, ...any[]] ? "continue" : "stop"];
}[P extends [any, ...any[]] ? "continue" : "stop"];

@@ -42,3 +42,3 @@ import { Concat } from "./concat";

*
* Contrary to `UnsafeMergeRec`, `SafeMergeRec` never returns never, but doesn't preserve non-required properties
* Contrary to `UnsafeMergeRec`, `SafeMergeRec` never returns `never`, but doesn't preserve non-required properties
*

@@ -45,0 +45,0 @@ * @param A Type

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