UNPKG

@samchon/openapi

Version:

Universal OpenAPI to LLM function calling schemas. Transform any Swagger/OpenAPI document into type-safe schemas for OpenAI, Claude, Qwen, and more.

41 lines (36 loc) 2.17 kB
import { NamingConvention } from "../NamingConvention.mjs"; import { OpenApiStationValidator } from "./OpenApiStationValidator.mjs"; var OpenApiObjectValidator; (function(OpenApiObjectValidator) { OpenApiObjectValidator.validate = ctx => { if (typeof ctx.value !== "object" || ctx.value === null) return ctx.report(ctx); return [ ...Object.entries(ctx.schema.properties ?? {}).map(([key, value]) => OpenApiStationValidator.validate({ ...ctx, schema: value, value: ctx.value[key], path: ctx.path + (NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`), required: ctx.schema.required?.includes(key) ?? false })), ...typeof ctx.schema.additionalProperties === "object" && ctx.schema.additionalProperties !== null ? Object.entries(ctx.value).filter(([key]) => Object.keys(ctx.schema.properties ?? {}).includes(key) === false).map(([key, value]) => OpenApiStationValidator.validate({ ...ctx, schema: ctx.schema.additionalProperties, value, path: ctx.path + (NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`), required: true })) : [], ...ctx.equals === true && (ctx.schema.additionalProperties ?? false) === false ? [ validateEquals(ctx) ] : [] ].every(v => v); }; const validateEquals = ctx => { const regular = new Set(Object.keys(ctx.schema.properties ?? {})); return Object.entries(ctx.value).map(([key, value]) => { if (regular.has(key) === true) return true; return ctx.report({ ...ctx, path: ctx.path + (NamingConvention.variable(key) ? `.${key}` : `[${JSON.stringify(key)}]`), value, expected: "undefined", description: [ `The property \`${key}\` is not defined in the object type.`, "", "Please remove the property next time." ].join("\n") }); }).every(v => v); }; })(OpenApiObjectValidator || (OpenApiObjectValidator = {})); export { OpenApiObjectValidator }; //# sourceMappingURL=OpenApiObjectValidator.mjs.map