@lobehub/ui
Version:
Lobe UI is an open-source UI component library for building AIGC web apps
1 lines • 2.79 kB
Source Map (JSON)
{"version":3,"file":"schema.mjs","names":[],"sources":["../../../src/base-ui/FormKit/schema.ts"],"sourcesContent":["import type { FieldValidator, FormValidator } from './engine/types';\n\nexport interface StandardSchemaIssue {\n message: string;\n path?: ReadonlyArray<PropertyKey | { key: PropertyKey }>;\n}\n\nexport interface StandardSchema<Input = unknown, Output = Input> {\n readonly '~standard': {\n readonly types?: { readonly input: Input; readonly output: Output };\n readonly validate: (\n value: unknown,\n ) =>\n | { readonly issues?: undefined; readonly value: Output }\n | { readonly issues: ReadonlyArray<StandardSchemaIssue> }\n | Promise<\n | { readonly issues?: undefined; readonly value: Output }\n | { readonly issues: ReadonlyArray<StandardSchemaIssue> }\n >;\n readonly vendor: string;\n readonly version: 1;\n };\n}\n\nexport type InferSchemaInput<S> = S extends StandardSchema<infer I, any> ? I : never;\n\nexport type FieldValidate =\n StandardSchema | ((value: any, values: any) => string | undefined | Promise<string | undefined>);\n\nexport const isStandardSchema = (value: unknown): value is StandardSchema =>\n typeof value === 'object' && value !== null && '~standard' in value;\n\nconst issuePath = (issue: StandardSchemaIssue) =>\n (issue.path ?? [])\n .map((segment) => String(typeof segment === 'object' ? segment.key : segment))\n .join('.');\n\nexport const schemaToFormValidator =\n (schema: StandardSchema): FormValidator =>\n async (values) => {\n const result = await schema['~standard'].validate(values);\n if (!result.issues) return undefined;\n const errors: Record<string, string> = {};\n for (const issue of result.issues) {\n const path = issuePath(issue);\n if (errors[path] === undefined) errors[path] = issue.message;\n }\n return errors;\n };\n\nexport const toFieldValidator = (validate: FieldValidate): FieldValidator => {\n if (!isStandardSchema(validate)) return validate;\n return async (value) => {\n const result = await validate['~standard'].validate(value);\n return result.issues?.[0]?.message;\n };\n};\n"],"mappings":";AA6BA,MAAa,oBAAoB,UAC/B,OAAO,UAAU,YAAY,UAAU,QAAQ,eAAe;AAEhE,MAAM,aAAa,WAChB,MAAM,QAAQ,CAAC,EAAA,CACb,KAAK,YAAY,OAAO,OAAO,YAAY,WAAW,QAAQ,MAAM,OAAO,CAAC,CAAC,CAC7E,KAAK,GAAG;AAEb,MAAa,yBACV,WACD,OAAO,WAAW;CAChB,MAAM,SAAS,MAAM,OAAO,YAAY,CAAC,SAAS,MAAM;CACxD,IAAI,CAAC,OAAO,QAAQ,OAAO,KAAA;CAC3B,MAAM,SAAiC,CAAC;CACxC,KAAK,MAAM,SAAS,OAAO,QAAQ;EACjC,MAAM,OAAO,UAAU,KAAK;EAC5B,IAAI,OAAO,UAAU,KAAA,GAAW,OAAO,QAAQ,MAAM;CACvD;CACA,OAAO;AACT;AAEF,MAAa,oBAAoB,aAA4C;CAC3E,IAAI,CAAC,iBAAiB,QAAQ,GAAG,OAAO;CACxC,OAAO,OAAO,UAAU;EAEtB,QAAO,MADc,SAAS,YAAY,CAAC,SAAS,KAAK,EAAA,CAC3C,SAAS,EAAE,EAAE;CAC7B;AACF"}