UNPKG

@autobe/agent

Version:

AI backend server code generator

206 lines (191 loc) 10.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.fulfillJsonSchemaErrorMessages = void 0; const utils_1 = require("@autobe/utils"); const typia_1 = __importDefault(require("typia")); const fulfillJsonSchemaErrorMessages = (errors) => { for (const e of errors) fulfillTypeAsArrayError(e) || fulfillEnumInsteadOfConstError(e) || fulfillNoRequiredError(e) || fulfillObjectMetadataMisplacement(e) || fulfillNestedObjectError(e) || fulfillJsonSchemaFormat(e); }; exports.fulfillJsonSchemaErrorMessages = fulfillJsonSchemaErrorMessages; const fulfillTypeAsArrayError = (e) => { if ( // type := ["number", "string", ...] case isInvalidJsonSchema(e) && (() => { const _io0 = input => Array.isArray(input.type) && input.type.every(elem => "string" === typeof elem); return input => "object" === typeof input && null !== input && _io0(input); })()(e.value) === true) { e.description = "<!--\nfilename: INTERFACE_SCHEMA_MISSING_TYPE_ARRAY.md\n-->\n**Invalid Schema: Array-type \"type\" property is not allowed.**\n\nYou defined the \"type\" property as an array (e.g., \\`[\"number\", \"string\"]\\`),\nbut the JSON schema specification requires \"type\" to be a single string value.\n\nTo represent a union of multiple types, you must use the \"oneOf\" construct.\nConvert your schema to the following format:\n\n```json\n${{JSON}}\n```\n\nYou must make this correction. The validator will continue to reject your\nschema until you replace the array-type \"type\" with a proper \"oneOf\" structure." /* AutoBeSystemPromptConstant.INTERFACE_SCHEMA_MISSING_TYPE_ARRAY */.replace("${{JSON}}", utils_1.StringUtil.trim ` { "oneOf": [ ${e.value.type.map((t) => ` { "type": ${JSON.stringify(t)}, ... },`).join("\n")} ],${"description" in e.value ? `\n "description": ${JSON.stringify(e.value.description)},` : ""} } `); return true; } return false; }; const fulfillEnumInsteadOfConstError = (e) => { if ( // enum to const isInvalidJsonSchema(e) && // biome-ignore lint: intended (() => { const _io0 = input => Array.isArray(input["enum"]); return input => "object" === typeof input && null !== input && _io0(input); })()(e.value) === true) { e.description = utils_1.StringUtil.trim ` **Invalid Schema: "enum" keyword is not supported in AutoBE.** The "enum" keyword is prohibited. AutoBE requires you to use the "oneOf" construct with individual "const" values instead. This design ensures better type safety and documentation clarity. Convert your schema to the following format: \`\`\`json { "oneOf": [ ${e.value.enum.map((t) => ` { "const": ${JSON.stringify(t)} },`).join("\n")} ],${"description" in e.value ? `\n "description": ${JSON.stringify(e.value.description)},` : ""} } \`\`\` You must make this correction. The validator will continue to reject your schema until you replace "enum" with the proper "oneOf" + "const" structure. `; return true; } return false; }; const fulfillNoRequiredError = (e) => { if ( // no required property e.value === undefined && e.path.endsWith(".required") && e.expected === "Array<string>") { e.description = "<!--\nfilename: INTERFACE_SCHEMA_MISSING_REQUIRED.md\n-->\n**Missing Required Field: \"required\"**\n\nEvery object type schema must have a \"required\" property that lists\nwhich property names are mandatory. This field must be present even\nwhen all properties are optional - in that case, provide an empty array.\n\nExample with required properties:\n```json\n{\n \"type\": \"object\",\n \"required\": [\"id\", \"name\", \"email\"],\n \"properties\": { ... }\n}\n```\n\nExample with no required properties:\n```json\n{\n \"type\": \"object\",\n \"required\": [],\n \"properties\": { ... }\n}\n```\n\nYou must add this field. The validator will continue to reject your schema\nuntil every object type has a \"required\" array." /* AutoBeSystemPromptConstant.INTERFACE_SCHEMA_MISSING_REQUIRED */; return true; } return false; }; const fulfillObjectMetadataMisplacement = (e) => { if (isInvalidJsonSchema(e) === false) return false; const validate = (props) => { e.expected = "undefined"; e.description = utils_1.StringUtil.trim ` **Structural Error: "${props.key}" is in the wrong location** You placed "${props.key}" inside the "properties" object, but it is a metadata field that belongs at the object type level, outside of "properties". - Your placement: \`${props.actual}\` - Correct placement: \`${props.expected}\` The "${props.key}" field describes ${props.purpose} and must be placed at the schema's top level alongside "type" and "properties". **Your current structure (incorrect)**: \`\`\`json { "type": "object", "properties": { ..., "${props.key}": ${JSON.stringify(e.value)} } } \`\`\` **Required structure**: \`\`\`json { "type": "object", "${props.key}": ${JSON.stringify(e.value)}, "properties": { ... } } \`\`\` Move "${props.key}" from ${e.path} to ${props.place}. The validator will continue to reject your schema until this structural correction is made. `; return true; }; if (e.path.endsWith(`.properties.required`) === true && Array.isArray(e.value) === true) return validate({ key: "required", expected: `AutoBeOpenApi.IJsonSchemaDescriptive.IObject.required`, actual: `AutoBeOpenApi.IJsonSchemaDescriptive.IObject.properties.required`, place: e.path.replace(`.properties.required`, `.required`), purpose: "which properties are mandatory", }); return false; }; const fulfillNestedObjectError = (e) => { if (isExcludedObjectType(e) === true) { // nested object e.description = "<!--\nfilename: INTERFACE_SCHEMA_MISSING_NESTED_OBJECT.md\n-->\n**Invalid Schema: Inline object types are not allowed**\n\nAutoBE prohibits nested inline object definitions. All object types must be\ndefined as named schemas in the components section and referenced via $ref.\nThis requirement enforces reusability and maintains the simplified AST structure.\n\n**Your current structure (invalid)**:\n```json\n{\n \"type\": \"array\",\n \"items\": { \"type\": \"object\", \"properties\": {...} }\n}\n```\n\n**Required approach**:\n1. Create a named schema in components.schemas (name must start with 'I'):\n```json\n\"IUserSummary\": {\n \"type\": \"object\",\n \"properties\": {...}\n}\n```\n\n2. Reference it using $ref:\n```json\n{\n \"type\": \"array\",\n \"items\": { \"$ref\": \"#/components/schemas/IUserSummary\" }\n}\n```\n\nThis rule applies wherever objects appear: array items, object properties,\nadditionalProperties, and oneOf variants. Extract every inline object\ndefinition to a named schema and replace it with a $ref.\n\nThe validator will continue to reject your schema until all inline object\ndefinitions are converted to named schema references." /* AutoBeSystemPromptConstant.INTERFACE_SCHEMA_MISSING_NESTED_OBJECT */; return true; } return false; }; const fulfillJsonSchemaFormat = (e) => { const supported = [ "password", "regex", "uuid", "email", "hostname", "idn-email", "idn-hostname", "iri", "iri-reference", "ipv4", "ipv6", "uri", "uri-reference", "uri-template", "url", "date-time", "date", "time", "duration", "json-pointer", "relative-json-pointer" ]; if (e.path.endsWith(".format") && typeof e.value === "string" && supported.every((s) => e.expected.includes(JSON.stringify(s)))) { e.expected = "undefined"; e.description = utils_1.StringUtil.trim ` **Invalid "format" Value: ${JSON.stringify(e.value)} is not supported.** The "format" property value ${JSON.stringify(e.value)} is not supported in the JSOn schema specification (type: \`AutoBeOpenApi.IJsonSchema.IString.format\`). Supported values are: ${supported.map((s) => `- ${s}`).join("\n")} If your intended format does not exactly match one of the above values, you must remove the "format" property entirely from this schema. Do not attempt to use similar or alternative format strings. Simply delete the "format" property — it is optional. The validator will continue to reject your schema until the "format" property is either removed or set to an exactly matching supported value. `; return true; } return false; }; const isExcludedObjectType = (error) => error.expected.includes("|") && ((error.expected.includes("AutoBeOpenApi.IJsonSchema.IConstant") && error.expected.includes("AutoBeOpenApi.IJsonSchema.IArray")) || (error.expected.includes("AutoBeOpenApi.IJsonSchemaDescriptive.IConstant") && error.expected.includes("AutoBeOpenApi.IJsonSchemaDescriptive.IArray")) || (error.expected.includes("AutoBeOpenApi.IJsonSchemaProperty.IConstant") && error.expected.includes("AutoBeOpenApi.IJsonSchemaProperty.IArray"))) && (() => { const _io0 = input => "object" === input.type; return input => "object" === typeof input && null !== input && _io0(input); })()(error.value) === true; const isInvalidJsonSchema = (e) => e.expected.startsWith("(") && e.expected.endsWith(")") && e.expected.includes("|") && e.expected .split("|") .map((s) => s.trim()) .some((s) => s.startsWith("AutoBeOpenApi.IJsonSchema.") || s.startsWith("AutoBeOpenApi.IJsonSchemaDescriptive.")); //# sourceMappingURL=fulfillJsonSchemaErrorMessages.js.map