|
| 1 | +import { |
| 2 | + type FieldConstraint, |
| 3 | + type FieldsetConstraint, |
| 4 | + type Submission, |
| 5 | + parse as baseParse, |
| 6 | +} from '@conform-to/dom'; |
| 7 | +import type { Static, StaticDecode, TObject, TSchema } from '@sinclair/typebox'; |
| 8 | +import { OptionalKind, TypeGuard } from '@sinclair/typebox'; |
| 9 | +import { Value, ValueErrorIterator } from '@sinclair/typebox/value'; |
| 10 | + |
| 11 | +function transformPath(path: string): string { |
| 12 | + const parts = path.split('/').filter(Boolean); // Split the string and remove empty parts |
| 13 | + return parts |
| 14 | + .map((part, index) => { |
| 15 | + // If the part is a number, format it as an array index, otherwise use a dot or nothing for the first part |
| 16 | + return isNaN(+part) ? (index === 0 ? part : `.${part}`) : `[${part}]`; |
| 17 | + }) |
| 18 | + .join(''); |
| 19 | +} |
| 20 | + |
| 21 | +export function getFieldsetConstraint<T extends TObject>( |
| 22 | + schema: T, |
| 23 | +): FieldsetConstraint<Static<T>> { |
| 24 | + function discardKey(value: Record<PropertyKey, any>, key: PropertyKey) { |
| 25 | + const { [key]: _, ...rest } = value; |
| 26 | + return rest; |
| 27 | + } |
| 28 | + function inferConstraint<T extends TSchema>(schema: T): FieldConstraint<T> { |
| 29 | + let constraint: FieldConstraint = {}; |
| 30 | + if (TypeGuard.IsOptional(schema)) { |
| 31 | + const unwrapped = discardKey(schema, OptionalKind) as TSchema; |
| 32 | + constraint = { |
| 33 | + ...inferConstraint(unwrapped), |
| 34 | + required: false, |
| 35 | + }; |
| 36 | + } else if (TypeGuard.IsArray(schema)) { |
| 37 | + constraint = { |
| 38 | + ...inferConstraint(schema.items), |
| 39 | + multiple: true, |
| 40 | + }; |
| 41 | + } else if (TypeGuard.IsString(schema)) { |
| 42 | + if (schema.minLength) { |
| 43 | + constraint.minLength = schema.minLength; |
| 44 | + } |
| 45 | + if (schema.maxLength) { |
| 46 | + constraint.maxLength = schema.maxLength; |
| 47 | + } |
| 48 | + if (schema.pattern) { |
| 49 | + constraint.pattern = schema.pattern; |
| 50 | + } |
| 51 | + } else if (TypeGuard.IsNumber(schema) || TypeGuard.IsInteger(schema)) { |
| 52 | + if (schema.minimum) { |
| 53 | + constraint.min = schema.minimum; |
| 54 | + } |
| 55 | + if (schema.maximum) { |
| 56 | + constraint.max = schema.maximum; |
| 57 | + } |
| 58 | + if (schema.multipleOf) { |
| 59 | + constraint.step = schema.multipleOf; |
| 60 | + } |
| 61 | + } else if (TypeGuard.IsUnionLiteral(schema)) { |
| 62 | + constraint.pattern = schema.anyOf |
| 63 | + .map((literal) => { |
| 64 | + const option = literal.const.toString(); |
| 65 | + // To escape unsafe characters on regex |
| 66 | + return option |
| 67 | + .replace(/[|\\{}()[\]^$+*?.]/g, '\\$&') |
| 68 | + .replace(/-/g, '\\x2d'); |
| 69 | + }) |
| 70 | + .join('|'); |
| 71 | + } |
| 72 | + |
| 73 | + if (typeof constraint.required === 'undefined') { |
| 74 | + constraint.required = true; |
| 75 | + } |
| 76 | + |
| 77 | + return constraint; |
| 78 | + } |
| 79 | + function resolveFieldsetConstraint<T extends TObject>( |
| 80 | + schema: T, |
| 81 | + ): FieldsetConstraint<Static<T>> { |
| 82 | + return Object.getOwnPropertyNames(schema.properties).reduce((acc, key) => { |
| 83 | + return { |
| 84 | + ...acc, |
| 85 | + [key]: inferConstraint( |
| 86 | + schema.properties[key as keyof FieldsetConstraint<Static<T>>], |
| 87 | + ), |
| 88 | + }; |
| 89 | + }, {} as FieldsetConstraint<Static<T>>); |
| 90 | + } |
| 91 | + |
| 92 | + return resolveFieldsetConstraint(schema); |
| 93 | +} |
| 94 | + |
| 95 | +export function parse<Schema extends TObject>( |
| 96 | + payload: FormData | URLSearchParams, |
| 97 | + config: { |
| 98 | + schema: Schema | ((intent: string) => Schema); |
| 99 | + }, |
| 100 | +): Submission<Static<Schema>>; |
| 101 | +export function parse<Schema extends TObject>( |
| 102 | + payload: FormData | URLSearchParams, |
| 103 | + config: { |
| 104 | + schema: Schema | ((intent: string) => Schema); |
| 105 | + }, |
| 106 | +): Submission<Static<Schema>> { |
| 107 | + return baseParse<StaticDecode<Schema>>(payload, { |
| 108 | + resolve(input, intent) { |
| 109 | + const schema = |
| 110 | + typeof config.schema === 'function' |
| 111 | + ? config.schema(intent) |
| 112 | + : config.schema; |
| 113 | + const resolveData = (value: Static<Schema>) => ({ value }); |
| 114 | + const resolveError = (error: unknown) => { |
| 115 | + if (error instanceof ValueErrorIterator) { |
| 116 | + return { |
| 117 | + error: Array.from(error).reduce((error, valueError) => { |
| 118 | + const path = transformPath(valueError.path); |
| 119 | + const innerError = (error[path] ??= []); |
| 120 | + innerError.push(valueError.message); |
| 121 | + return error; |
| 122 | + }, {} as Record<string, string[]>), |
| 123 | + }; |
| 124 | + } |
| 125 | + |
| 126 | + throw error; |
| 127 | + }; |
| 128 | + |
| 129 | + // coerce the input to the schema |
| 130 | + const payload = Value.Convert(schema, input); |
| 131 | + try { |
| 132 | + return resolveData(Value.Decode(schema, payload)); |
| 133 | + } catch (error) { |
| 134 | + return resolveError(Value.Errors(schema, payload)); |
| 135 | + } |
| 136 | + }, |
| 137 | + }); |
| 138 | +} |
0 commit comments