Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 06:00 UTC
| 1 | import { isAssertedStringFormat } from '../annotations'; |
| 2 | import type { ParseContext } from '../context'; |
| 3 | import type { JsonSchemaObject } from '../types'; |
| 4 | import { compilePattern } from '../utils/compile-pattern'; |
| 5 | import { extendSchemaWithMessage } from '../utils/extend-schema'; |
| 6 | |
| 7 | export const parseString = <S>( |
| 8 | jsonSchema: JsonSchemaObject & { type: 'string' }, |
| 9 | ctx: Pick<ParseContext<S>, 'path' | 'backend'> |
| 10 | ): S => { |
| 11 | const { backend } = ctx; |
| 12 | let zodSchema = backend.string(); |
| 13 | |
| 14 | zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, 'format', (zs, format, errorMsg) => |
| 15 | isAssertedStringFormat(format) ? backend.stringFormat(zs, format, errorMsg) : zs |
| 16 | ); |
| 17 | |
| 18 | // `contentEncoding` names how the string encodes its bytes; only base64 is |
| 19 | // checked. Other encodings (base16, base32, quoted-printable, ...) are left |
| 20 | // unvalidated, and `format: "binary"` (OpenAPI: any sequence of octets) is |
| 21 | // not an encoding at all. Encoding names are case-insensitive (RFC 2045). |
| 22 | zodSchema = extendSchemaWithMessage( |
| 23 | zodSchema, |
| 24 | jsonSchema, |
| 25 | 'contentEncoding', |
| 26 | (zs, encoding, errorMsg) => (isBase64Encoding(encoding) ? backend.base64(zs, errorMsg) : zs) |
| 27 | ); |
| 28 | zodSchema = extendSchemaWithMessage(zodSchema, jsonSchema, 'pattern', (zs, pattern, errorMsg) => |
| 29 | backend.regex(zs, compilePattern('pattern', pattern, ctx), errorMsg) |
| 30 | ); |
| 31 | // JSON Schema length constraints count Unicode code points, while Zod's |
| 32 | // built-in `.min()`/`.max()` count UTF-16 code units and overcount astral |
| 33 | // glyphs, so both bounds are applied as code-point refinements. |
| 34 | const errorMessages = (jsonSchema as { errorMessage?: Record<string, string> }).errorMessage; |
| 35 | // 'min'/'max' are generic aliases for 'minLength'/'maxLength' |
| 36 | const minLength = |
| 37 | typeof jsonSchema.minLength === 'number' |
| 38 | ? jsonSchema.minLength |
| 39 | : typeof jsonSchema.min === 'number' |
| 40 | ? jsonSchema.min |
| 41 | : undefined; |
| 42 | const maxLength = |
| 43 | typeof jsonSchema.maxLength === 'number' |
| 44 | ? jsonSchema.maxLength |
| 45 | : typeof jsonSchema.max === 'number' |
| 46 | ? jsonSchema.max |
| 47 | : undefined; |
| 48 | |
| 49 | let result = zodSchema; |
| 50 | if (minLength !== undefined) { |
| 51 | result = backend.refine<string>( |
| 52 | result, |
| 53 | value => codePointLength(value) >= minLength, |
| 54 | errorMessages?.minLength ?? `String must contain at least ${minLength} character(s)` |
| 55 | ); |
| 56 | } |
| 57 | if (maxLength !== undefined) { |
| 58 | result = backend.refine<string>( |
| 59 | result, |
| 60 | value => codePointLength(value) <= maxLength, |
| 61 | errorMessages?.maxLength ?? `String must contain at most ${maxLength} character(s)` |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | return result; |
| 66 | }; |
| 67 | |
| 68 | const codePointLength = (value: string): number => [...value].length; |
| 69 | |
| 70 | const isBase64Encoding = (encoding: unknown): boolean => |
| 71 | typeof encoding === 'string' && encoding.toLowerCase() === 'base64'; |