Skip to content

Commit

Permalink
fix(types): Add looser types to input schema (#61)
Browse files Browse the repository at this point in the history
* fix(types): Add looser types to input schema

* fix(types): Add looser types to input schema
  • Loading branch information
jonluca authored Apr 4, 2023
1 parent e21a5bf commit 28686f4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import deepEqual from "fast-deep-equal";
import { fromSchema, fromParameter } from "./lib/convert";
import type { Options, OptionsInternal, OpenAPI3 } from "./openapi-schema-types";
import type { Options, OptionsInternal } from "./openapi-schema-types";
import { NOT_SUPPORTED, STRUCTS } from "./consts";
import type { JSONSchema4 } from "json-schema";
import type { ParameterObject, ResponseObject } from "openapi-typescript/src/types";
import { cloneDeep } from "./lib/utils/cloneDeep";
import type { AcceptibleInputSchema } from "./openapi-schema-types";

const patternPropertiesHandler = (schema) => {
let pattern;
const patternsObj = schema.patternProperties;
Expand Down Expand Up @@ -56,7 +58,10 @@ const resolveOptions = (_options?: Options): OptionsInternal => {
return options;
};

const openapiSchemaToJsonSchema = <T extends OpenAPI3 = OpenAPI3>(schema: T, options?: Options): JSONSchema4 => {
const openapiSchemaToJsonSchema = <T extends AcceptibleInputSchema = AcceptibleInputSchema>(
schema: T,
options?: Options,
): JSONSchema4 => {
const optionsInternal = resolveOptions(options);
return fromSchema<T>(schema, optionsInternal);
};
Expand Down
6 changes: 5 additions & 1 deletion src/lib/converters/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import type { PatternPropertiesHandler } from "../../openapi-schema-types";
import type { OpenAPI3 } from "openapi-typescript";
import type { ReferenceObject } from "openapi-typescript/src/types";
import { cloneDeep } from "../utils/cloneDeep";
import type { AcceptibleInputSchema } from "../../openapi-schema-types";

// Convert from OpenAPI 3.0 `SchemaObject` to JSON schema v4
function convertFromSchema<T extends OpenAPI3 = OpenAPI3>(schema: T, options: OptionsInternal): JSONSchema4 {
function convertFromSchema<T extends AcceptibleInputSchema = AcceptibleInputSchema>(
schema: T,
options: OptionsInternal,
): JSONSchema4 {
const newSchema = convertSchema(schema, options);
(<JSONSchema4>newSchema).$schema = "http://json-schema.org/draft-04/schema#";
return newSchema;
Expand Down
1 change: 1 addition & 0 deletions src/openapi-schema-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { JSONSchema4 } from "json-schema";
export type { OpenAPI3 };
// We don't know what the shape of the object looks like when it's passed in, but we know its some mix of these two
export type PatternPropertiesHandler = (schema: SchemaObject) => SchemaObject;
export type AcceptibleInputSchema = SchemaObject | OpenAPI3 | Record<string, any>;

export interface Options {
dateToDateTime?: boolean;
Expand Down

0 comments on commit 28686f4

Please sign in to comment.