Skip to content

fix(types): Add looser types to input schema #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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