@kmudrick/io-ts-openapi
Advanced tools
Comparing version
@@ -54,3 +54,7 @@ import * as t from "io-ts"; | ||
export declare const ArraySchema: t.Type<ArraySchema>; | ||
export declare type JSONSchema = StringSchema | NumberSchema | IntegerSchema | BooleanSchema | NullSchema | ObjectSchema | ArraySchema; | ||
export interface OneOfSchema { | ||
oneOf: Array<JSONSchema>; | ||
} | ||
export declare const OneOfSchema: t.Type<OneOfSchema>; | ||
export declare type JSONSchema = StringSchema | NumberSchema | IntegerSchema | BooleanSchema | NullSchema | ObjectSchema | ArraySchema | OneOfSchema; | ||
export declare const JSONSchema: t.Type<JSONSchema>; |
@@ -26,3 +26,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.JSONSchema = exports.ArraySchema = exports.ObjectSchema = exports.NullSchema = exports.BooleanSchema = exports.IntegerSchema = exports.NumberSchema = exports.StringSchema = void 0; | ||
exports.JSONSchema = exports.OneOfSchema = exports.ArraySchema = exports.ObjectSchema = exports.NullSchema = exports.BooleanSchema = exports.IntegerSchema = exports.NumberSchema = exports.StringSchema = void 0; | ||
const t = __importStar(require("io-ts")); | ||
@@ -85,3 +85,3 @@ // io-ts codecs that describe a JSONSchema Document | ||
t.partial({ | ||
tems: t.array(exports.JSONSchema), | ||
items: exports.JSONSchema, | ||
minItems: t.number, | ||
@@ -91,2 +91,5 @@ maxItems: t.number, | ||
])); | ||
exports.OneOfSchema = t.recursion("OneOfSchema", () => t.type({ | ||
oneOf: t.array(exports.JSONSchema), | ||
})); | ||
exports.JSONSchema = t.recursion("JSONSchema", () => t.union([ | ||
@@ -100,3 +103,4 @@ exports.StringSchema, | ||
exports.ArraySchema, | ||
exports.OneOfSchema, | ||
])); | ||
//# sourceMappingURL=json-schema.js.map |
@@ -28,2 +28,3 @@ "use strict"; | ||
const t = __importStar(require("io-ts-codegen")); | ||
const json_schema_1 = require("./json-schema"); | ||
function toInterfaceCombinator(schema) { | ||
@@ -42,18 +43,24 @@ const combinator = t.typeCombinator(Object.keys(schema.properties).map((key) => t.property(key, toTypeReference(schema.properties[key]), schema.required?.includes(key) ?? false))); | ||
function toTypeReference(schema) { | ||
switch (schema.type) { | ||
case "string": | ||
return schema.enum ? t.keyofCombinator(schema.enum) : t.stringType; | ||
case "number": | ||
return t.numberType; | ||
case "integer": | ||
return t.numberType; | ||
case "boolean": | ||
return t.booleanType; | ||
case "object": | ||
return toInterfaceCombinator(schema); | ||
case "null": | ||
return t.nullType; | ||
case "array": | ||
return toArrayCombinator(schema); | ||
if (json_schema_1.OneOfSchema.is(schema)) { | ||
const typeRefs = schema.oneOf.map(toTypeReference); | ||
return t.unionCombinator(typeRefs); | ||
} | ||
else { | ||
switch (schema.type) { | ||
case "string": | ||
return schema.enum ? t.keyofCombinator(schema.enum) : t.stringType; | ||
case "number": | ||
return t.numberType; | ||
case "integer": | ||
return t.numberType; | ||
case "boolean": | ||
return t.booleanType; | ||
case "object": | ||
return toInterfaceCombinator(schema); | ||
case "null": | ||
return t.nullType; | ||
case "array": | ||
return toArrayCombinator(schema); | ||
} | ||
} | ||
} | ||
@@ -60,0 +67,0 @@ exports.toTypeReference = toTypeReference; |
{ | ||
"name": "@kmudrick/io-ts-openapi", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"license": "MIT", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -156,3 +156,3 @@ # @kmudrick/io-ts-openapi | ||
import * as t from "io-ts"; | ||
interface Pet { | ||
export interface Pet { | ||
id?: number; | ||
@@ -162,3 +162,3 @@ name?: string; | ||
} | ||
type Pets = Array<{ | ||
export type Pets = Array<{ | ||
id?: number; | ||
@@ -168,7 +168,7 @@ name?: string; | ||
}>; | ||
interface Error { | ||
export interface Error { | ||
code?: number; | ||
message?: string; | ||
} | ||
const Pet = t.intersection([ | ||
export const Pet = t.intersection([ | ||
t.type({ | ||
@@ -182,3 +182,3 @@ tag: t.string, | ||
]); | ||
const Pets = t.array( | ||
export const Pets = t.array( | ||
t.intersection([ | ||
@@ -194,3 +194,3 @@ t.type({ | ||
); | ||
const Error = t.partial({ | ||
export const Error = t.partial({ | ||
code: t.number, | ||
@@ -197,0 +197,0 @@ message: t.string, |
@@ -96,3 +96,3 @@ import * as t from "io-ts"; | ||
t.partial({ | ||
tems: t.array(JSONSchema), | ||
items: JSONSchema, | ||
minItems: t.number, | ||
@@ -104,2 +104,13 @@ maxItems: t.number, | ||
export interface OneOfSchema { | ||
// todo support type | ||
oneOf: Array<JSONSchema>; | ||
} | ||
export const OneOfSchema: t.Type<OneOfSchema> = t.recursion("OneOfSchema", () => | ||
t.type({ | ||
oneOf: t.array(JSONSchema), | ||
}) | ||
); | ||
export type JSONSchema = | ||
@@ -112,3 +123,4 @@ | StringSchema | ||
| ObjectSchema | ||
| ArraySchema; | ||
| ArraySchema | ||
| OneOfSchema; | ||
@@ -124,3 +136,4 @@ export const JSONSchema: t.Type<JSONSchema> = t.recursion("JSONSchema", () => | ||
ArraySchema, | ||
OneOfSchema, | ||
]) | ||
); |
import * as t from "io-ts-codegen"; | ||
import { ArraySchema, JSONSchema, ObjectSchema } from "./json-schema"; | ||
import { | ||
ArraySchema, | ||
JSONSchema, | ||
ObjectSchema, | ||
OneOfSchema, | ||
} from "./json-schema"; | ||
@@ -28,17 +33,22 @@ function toInterfaceCombinator(schema: ObjectSchema): t.InterfaceCombinator { | ||
export function toTypeReference(schema: JSONSchema): t.TypeReference { | ||
switch (schema.type) { | ||
case "string": | ||
return schema.enum ? t.keyofCombinator(schema.enum) : t.stringType; | ||
case "number": | ||
return t.numberType; | ||
case "integer": | ||
return t.numberType; | ||
case "boolean": | ||
return t.booleanType; | ||
case "object": | ||
return toInterfaceCombinator(schema); | ||
case "null": | ||
return t.nullType; | ||
case "array": | ||
return toArrayCombinator(schema); | ||
if (OneOfSchema.is(schema)) { | ||
const typeRefs = schema.oneOf.map(toTypeReference); | ||
return t.unionCombinator(typeRefs); | ||
} else { | ||
switch (schema.type) { | ||
case "string": | ||
return schema.enum ? t.keyofCombinator(schema.enum) : t.stringType; | ||
case "number": | ||
return t.numberType; | ||
case "integer": | ||
return t.numberType; | ||
case "boolean": | ||
return t.booleanType; | ||
case "object": | ||
return toInterfaceCombinator(schema); | ||
case "null": | ||
return t.nullType; | ||
case "array": | ||
return toArrayCombinator(schema); | ||
} | ||
} | ||
@@ -45,0 +55,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
41153
3.48%729
5.19%