Open History to see the last commit
1import { isNil } from './utils.lib'
2import { createOutput } from './validationOutput.lib'
3import type {
4 StandardSchemaV1,
5 StandardSchemaV1Issue,
6} from './standardSchema.public'
7import type {
8 FieldValidatorFn,
9 FormGroupValidatorFn,
10 FormValidatorFn,
11 ParsedStandardSchemaIssues,
12 ValidationOutput,
13} from './validation.public'
14β
15export function prefixSchemaToErrors(
16 issues: ReadonlyArray<StandardSchemaV1Issue>,
17 formValue: unknown,
18) {
19 const schema = new Map<string, Array<StandardSchemaV1Issue>>()
20β
21 for (const issue of issues) {
22 const path = getPathFromIssue(issue, formValue)
23 if (path === '') continue
24β
25 schema.set(path, (schema.get(path) ?? []).concat(issue))
26 }
27β
28 return Object.fromEntries(schema)
29}
30β
31function getPathFromIssue(
32 issue: StandardSchemaV1Issue,
33 formValue: unknown,
34): string {
35 const issuePath = issue.path ?? []
36β
37 let currentFormValue = formValue
38 let path = ''
39β
40 for (const pathSegment of issuePath) {
41 if (isNil(pathSegment)) continue
42β
43 const segment =
44 typeof pathSegment === 'object' ? pathSegment.key : pathSegment
45 if (segment === '') continue
46β
47 // Standard Schema doesn't specify if paths should use numbers or stringified numbers for array access.
48 // However, if we follow the path it provides and encounter an array, then we can assume it's intended for array access.
49 const segmentAsNumber = Number(segment)
50 if (Array.isArray(currentFormValue) && !Number.isNaN(segmentAsNumber)) {
51 path += `[${segmentAsNumber}]`
52 } else {
53 path += (path === '' ? '' : '.') + String(segment)
54 }
55β
56 if (typeof currentFormValue === 'object' && currentFormValue !== null) {
57 currentFormValue = currentFormValue[segment as never]
58 } else {
59 currentFormValue = undefined
60 }
61 }
62β
63 return path
64}
65β
66export function isStandardSchema(
67 object:
68 | FormValidatorFn<any>
69 | FormGroupValidatorFn<any>
70 | FieldValidatorFn<any, any, any>
71 | StandardSchemaV1,
72): object is StandardSchemaV1<any, any> {
73 // Arktype typeof is function, not object. This is the only constraint that's safe so far.
74 return '~standard' in object
75}
76β
77export function parseStandardSchema<TOutput>(
78 schema: StandardSchemaV1<any, TOutput>,
79 value: any,
80 scope: 'form' | 'field',
81): Promise<
82 | ValidationOutput<TOutput>
83 | ParsedStandardSchemaIssues<any>
84 | Array<StandardSchemaV1Issue>
85> {
86 return Promise.resolve(schema['~standard'].validate(value)).then((result) => {
87 if (!result.issues) {
88 return createOutput(result.value)
89 }
90β
91 return parseStandardSchemaIssues(result.issues, value, scope)
92 })
93}
94β
95export function parseStandardSchemaIssues<TValue>(
96 issues: ReadonlyArray<StandardSchemaV1Issue>,
97 value: TValue,
98 scope: 'form',
99): ParsedStandardSchemaIssues<TValue>
100export function parseStandardSchemaIssues(
101 issues: ReadonlyArray<StandardSchemaV1Issue>,
102 value: unknown,
103 scope: 'field',
104): Array<StandardSchemaV1Issue>
105export function parseStandardSchemaIssues<TValue>(
106 issues: ReadonlyArray<StandardSchemaV1Issue>,
107 value: TValue,
108 scope: 'form' | 'field',
109): ParsedStandardSchemaIssues<TValue> | Array<StandardSchemaV1Issue>
110export function parseStandardSchemaIssues<TValue>(
111 issues: ReadonlyArray<StandardSchemaV1Issue>,
112 value: TValue,
113 scope: 'form' | 'field',
114): ParsedStandardSchemaIssues<TValue> | Array<StandardSchemaV1Issue> {
115 if (scope === 'field') {
116 return issues.slice()
117 }
118β
119 return {
120 form: issues.slice(),
121 fields: prefixSchemaToErrors(
122 issues,
123 value,
124 ) as ParsedStandardSchemaIssues<TValue>['fields'],
125 }
126}