json-schema-to-ts
Advanced tools
Comparing version 0.1.6 to 0.1.7
{ | ||
"name": "json-schema-to-ts", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Infer typescript types from your JSON schemas!", | ||
@@ -5,0 +5,0 @@ "main": "src/index.ts", |
@@ -100,18 +100,2 @@ # Stop typing twice 🙅♂️ | ||
### Objects | ||
```typescript | ||
const objectSchema = { | ||
type: "object", | ||
properties: { | ||
foo: { type: "string" }, | ||
bar: { type: "number" }, | ||
}, | ||
required: ["foo"], | ||
} as const; | ||
type Object = FromSchema<typeof objectSchema>; | ||
// => { foo: string, bar?: number } | ||
``` | ||
### Arrays | ||
@@ -154,2 +138,59 @@ | ||
### Objects | ||
```typescript | ||
const objectSchema = { | ||
type: "object", | ||
properties: { | ||
foo: { type: "string" }, | ||
bar: { type: "number" }, | ||
}, | ||
required: ["foo"], | ||
} as const; | ||
type Object = FromSchema<typeof objectSchema>; | ||
// => { foo: string, bar?: number } | ||
``` | ||
`FromSchema` partially supports the use of the `additionalProperties` and `patternProperties` keyword: | ||
- Contrary to the specifications, `additionalProperties` is considered `false` by default for clearer typings. Set its value to `true` to signal that additional properties can be used: | ||
```typescript | ||
const additionalPropertiesSchema = { | ||
...objectSchema, | ||
additionalProperties: true, | ||
} as const; | ||
type Object = FromSchema<typeof additionalPropertiesSchema>; | ||
// => { [x: string]: any; foo: string; bar?: number } | ||
``` | ||
- Used on their own, typed `additionalProperties` and/or `patternProperties` are supported: | ||
```typescript | ||
const typedValuesSchema = { | ||
type: "object", | ||
additionalProperties: { | ||
type: "boolean", | ||
}, | ||
} as const; | ||
type Object = FromSchema<typeof patternSchema>; | ||
// => { [key: string]: boolean } | ||
const patternSchema = { | ||
type: "object", | ||
patternProperties: { | ||
"^S": { type: "string" }, | ||
"^I": { type: "integer" }, | ||
}, | ||
} as const; | ||
type Object = FromSchema<typeof patternSchema>; | ||
// => { [key: string]: string | number } | ||
``` | ||
- However, due to [TypeScript limitations](https://github.com/Microsoft/TypeScript/issues/7599), when used in combination with the `properties` keyword, extra properties will always be typed as `any` to avoid conflicts with base properties. | ||
### Multiple Types | ||
@@ -156,0 +197,0 @@ |
@@ -7,24 +7,66 @@ import { FromWriteableSchema } from "./index"; | ||
? "properties" extends keyof S | ||
? number extends keyof S["required"] | ||
? MergeRight< | ||
{ | ||
[key in Exclude< | ||
keyof S["properties"], | ||
S["required"][number] | ||
>]?: S["properties"][key] extends Schema | ||
? FromWriteableSchema<S["properties"][key]> | ||
: never; | ||
}, | ||
{ | ||
[key in S["required"][number]]: FromWriteableSchema< | ||
S["properties"][key] | ||
>; | ||
} | ||
? MergeRight< | ||
"additionalProperties" extends keyof S | ||
? S["additionalProperties"] extends true | object | ||
? { [key: string]: any } | ||
: {} | ||
: {}, | ||
number extends keyof S["required"] | ||
? MergeRight< | ||
{ | ||
[key in Exclude< | ||
keyof S["properties"], | ||
S["required"][number] | ||
>]?: S["properties"][key] extends Schema | ||
? FromWriteableSchema<S["properties"][key]> | ||
: never; | ||
}, | ||
{ | ||
[key in S["required"][number]]: FromWriteableSchema< | ||
S["properties"][key] | ||
>; | ||
} | ||
> | ||
: { | ||
[key in keyof S["properties"]]?: S["properties"][key] extends Schema | ||
? FromWriteableSchema<S["properties"][key]> | ||
: never; | ||
} | ||
> | ||
: "additionalProperties" extends keyof S | ||
? "patternProperties" extends keyof S | ||
? AdditionalAndPatternProps< | ||
S["additionalProperties"], | ||
S["patternProperties"] | ||
> | ||
: { | ||
[key in keyof S["properties"]]?: S["properties"][key] extends Schema | ||
? FromWriteableSchema<S["properties"][key]> | ||
: never; | ||
} | ||
: AdditionalProps<S["additionalProperties"]> | ||
: "patternProperties" extends keyof S | ||
? PatternProps<S["patternProperties"]> | ||
: object | ||
: never; | ||
type AdditionalProps<A> = A extends false | ||
? { [key: string]: never } | ||
: A extends true | ||
? { [key: string]: any } | ||
: A extends Schema | ||
? { [key: string]: FromWriteableSchema<A> } | ||
: {}; | ||
type PatternProps<P> = { | ||
[key: string]: { | ||
[key in keyof P]: FromWriteableSchema<P[key]>; | ||
}[keyof P]; | ||
}; | ||
type AdditionalAndPatternProps<A, P> = A extends boolean | ||
? PatternProps<P> | ||
: A extends Schema | ||
? { | ||
[key: string]: | ||
| FromWriteableSchema<A> | ||
| { | ||
[key in keyof P]: FromWriteableSchema<P[key]>; | ||
}[keyof P]; | ||
} | ||
: never; |
@@ -28,2 +28,3 @@ import { MergeRight } from "./utils"; | ||
pattern?: string; | ||
format?: string; | ||
}>; | ||
@@ -53,5 +54,8 @@ | ||
required?: any[]; | ||
dependentRequired?: { [property: string]: string[] }; | ||
maxProperties?: number; | ||
minProperties?: number; | ||
additionalProperties?: boolean | Schema; | ||
propertyNames?: StringSchema; | ||
patternProperties?: { [pattern: string]: Schema }; | ||
dependencies?: { [property: string]: string[] | object }; | ||
}>; | ||
@@ -58,0 +62,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19010
364
273