UNPKG

@samchon/openapi

Version:

OpenAPI definitions and converters for 'typia' and 'nestia'.

1 lines 6.67 kB
{"version":3,"file":"OpenApiConstraintShifter.mjs","sources":["../../src/utils/OpenApiConstraintShifter.ts"],"sourcesContent":["import { OpenApi } from \"../OpenApi\";\nimport { OpenApiExclusiveEmender } from \"./OpenApiExclusiveEmender\";\n\nexport namespace OpenApiConstraintShifter {\n export const shiftArray = <\n Schema extends Pick<\n OpenApi.IJsonSchema.IArray,\n \"description\" | \"minItems\" | \"maxItems\" | \"uniqueItems\"\n >,\n >(\n schema: Schema,\n ): Omit<Schema, \"minItems\" | \"maxItems\" | \"uniqueItems\"> => {\n const tags: string[] = [];\n if (schema.minItems !== undefined) {\n tags.push(`@minItems ${schema.minItems}`);\n delete schema.minItems;\n }\n if (schema.maxItems !== undefined) {\n tags.push(`@maxItems ${schema.maxItems}`);\n delete schema.maxItems;\n }\n if (schema.uniqueItems !== undefined) {\n if (schema.uniqueItems === true) tags.push(`@uniqueItems`);\n delete schema.uniqueItems;\n }\n schema.description = writeTagWithDescription({\n description: schema.description,\n tags,\n });\n return schema;\n };\n\n export const shiftNumeric = <\n Schema extends Pick<\n OpenApi.IJsonSchema.INumber | OpenApi.IJsonSchema.IInteger,\n | \"description\"\n | \"minimum\"\n | \"maximum\"\n | \"exclusiveMinimum\"\n | \"exclusiveMaximum\"\n | \"multipleOf\"\n | \"default\"\n >,\n >(\n schema: Schema,\n ): Omit<\n Schema,\n | \"minimum\"\n | \"maximum\"\n | \"exclusiveMinimum\"\n | \"exclusiveMaximum\"\n | \"multipleOf\"\n | \"default\"\n > => {\n Object.assign(OpenApiExclusiveEmender.emend(schema));\n\n const tags: string[] = [];\n if (schema.minimum !== undefined) {\n tags.push(`@minimum ${schema.minimum}`);\n delete schema.minimum;\n }\n if (schema.maximum !== undefined) {\n tags.push(`@maximum ${schema.maximum}`);\n delete schema.maximum;\n }\n if (schema.exclusiveMinimum !== undefined) {\n tags.push(`@exclusiveMinimum ${schema.exclusiveMinimum}`);\n delete schema.exclusiveMinimum;\n }\n if (schema.exclusiveMaximum !== undefined) {\n tags.push(`@exclusiveMaximum ${schema.exclusiveMaximum}`);\n delete schema.exclusiveMaximum;\n }\n if (schema.multipleOf !== undefined) {\n tags.push(`@multipleOf ${schema.multipleOf}`);\n delete schema.multipleOf;\n }\n schema.description = writeTagWithDescription({\n description: schema.description,\n tags,\n });\n if (schema.default !== undefined) {\n tags.push(`@default ${schema.default}`);\n delete schema.default;\n }\n return schema;\n };\n\n export const shiftString = <\n Schema extends Pick<\n OpenApi.IJsonSchema.IString,\n | \"description\"\n | \"minLength\"\n | \"maxLength\"\n | \"format\"\n | \"pattern\"\n | \"contentMediaType\"\n | \"default\"\n >,\n >(\n schema: Schema,\n ): Omit<\n Schema,\n | \"minLength\"\n | \"maxLength\"\n | \"format\"\n | \"pattern\"\n | \"contentMediaType\"\n | \"default\"\n > => {\n const tags: string[] = [];\n if (schema.minLength !== undefined) {\n tags.push(`@minLength ${schema.minLength}`);\n delete schema.minLength;\n }\n if (schema.maxLength !== undefined) {\n tags.push(`@maxLength ${schema.maxLength}`);\n delete schema.maxLength;\n }\n if (schema.format !== undefined) {\n tags.push(`@format ${schema.format}`);\n delete schema.format;\n }\n if (schema.pattern !== undefined) {\n tags.push(`@pattern ${schema.pattern}`);\n delete schema.pattern;\n }\n if (schema.contentMediaType !== undefined) {\n tags.push(`@contentMediaType ${schema.contentMediaType}`);\n delete schema.contentMediaType;\n }\n if (schema.default !== undefined) {\n tags.push(`@default ${schema.default}`);\n delete schema.default;\n }\n schema.description = writeTagWithDescription({\n description: schema.description,\n tags,\n });\n return schema;\n };\n}\n\nconst writeTagWithDescription = (props: {\n description: string | undefined;\n tags: string[];\n}): string | undefined => {\n if (props.tags.length === 0) return props.description;\n return [\n ...(props.description?.length ? [props.description, \"\\n\"] : []),\n ...props.tags,\n ].join(\"\\n\");\n};\n"],"names":["OpenApiConstraintShifter","shiftArray","schema","tags","minItems","undefined","push","maxItems","uniqueItems","description","writeTagWithDescription","shiftNumeric","Object","assign","OpenApiExclusiveEmender","emend","minimum","maximum","exclusiveMinimum","exclusiveMaximum","multipleOf","default","shiftString","minLength","maxLength","format","pattern","contentMediaType","props","length","join"],"mappings":";;AAGM,IAAWA;;CAAjB,SAAiBA;IACFA,yBAAAC,aAMXC;QAEA,MAAMC,OAAiB;QACvB,IAAID,OAAOE,aAAaC,WAAW;YACjCF,KAAKG,KAAK,aAAaJ,OAAOE;mBACvBF,OAAOE;;QAEhB,IAAIF,OAAOK,aAAaF,WAAW;YACjCF,KAAKG,KAAK,aAAaJ,OAAOK;mBACvBL,OAAOK;;QAEhB,IAAIL,OAAOM,gBAAgBH,WAAW;YACpC,IAAIH,OAAOM,gBAAgB,MAAML,KAAKG,KAAK;mBACpCJ,OAAOM;;QAEhBN,OAAOO,cAAcC,wBAAwB;YAC3CD,aAAaP,OAAOO;YACpBN;;QAEF,OAAOD;AAAM;IAGFF,yBAAAW,eAYXT;QAUAU,OAAOC,OAAOC,wBAAwBC,MAAMb;QAE5C,MAAMC,OAAiB;QACvB,IAAID,OAAOc,YAAYX,WAAW;YAChCF,KAAKG,KAAK,YAAYJ,OAAOc;mBACtBd,OAAOc;;QAEhB,IAAId,OAAOe,YAAYZ,WAAW;YAChCF,KAAKG,KAAK,YAAYJ,OAAOe;mBACtBf,OAAOe;;QAEhB,IAAIf,OAAOgB,qBAAqBb,WAAW;YACzCF,KAAKG,KAAK,qBAAqBJ,OAAOgB;mBAC/BhB,OAAOgB;;QAEhB,IAAIhB,OAAOiB,qBAAqBd,WAAW;YACzCF,KAAKG,KAAK,qBAAqBJ,OAAOiB;mBAC/BjB,OAAOiB;;QAEhB,IAAIjB,OAAOkB,eAAef,WAAW;YACnCF,KAAKG,KAAK,eAAeJ,OAAOkB;mBACzBlB,OAAOkB;;QAEhBlB,OAAOO,cAAcC,wBAAwB;YAC3CD,aAAaP,OAAOO;YACpBN;;QAEF,IAAID,OAAOmB,YAAYhB,WAAW;YAChCF,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