Open History to see the last commit
1import { describe, expect, it, vi } from 'vitest'
2import {
3 isStandardSchema,
4 parseStandardSchema,
5 prefixSchemaToErrors,
6} from '../src/standardSchema.lib'
7import { VALIDATION_OUTPUT } from '../src/validationOutput.lib'
8import type {
9 StandardSchemaV1,
10 StandardSchemaV1Issue,
11} from '../src/standardSchema.public'
12β
13function getSchema<TOutput>(
14 validate: StandardSchemaV1<unknown, TOutput>['~standard']['validate'],
15): StandardSchemaV1<unknown, TOutput> {
16 return {
17 '~standard': {
18 version: 1,
19 vendor: 'test',
20 validate,
21 },
22 }
23}
24β
25describe('standard schema validation', () => {
26 describe('prefixSchemaToErrors', () => {
27 it('does not use an empty string as a field path for root issues', () => {
28 const issues = [
29 { message: 'Invalid form' },
30 { message: 'Still invalid', path: [] },
31 { message: 'Empty string paths are root errors', path: [''] },
32 ] satisfies Array<StandardSchemaV1Issue>
33β
34 expect(prefixSchemaToErrors(issues, {})).toEqual({})
35 })
36β
37 it('prefixes nested object paths and preserves multiple issues for the same field', () => {
38 const issues = [
39 { message: 'Name is required', path: ['user', 'name'] },
40 { message: 'Name is too short', path: ['user', { key: 'name' }] },
41 ] satisfies Array<StandardSchemaV1Issue>
42β
43 expect(prefixSchemaToErrors(issues, { user: { name: '' } })).toEqual({
44 'user.name': issues,
45 })
46 })
47β
48 it('uses bracket notation for array indexes from number or string path segments', () => {
49 const issues = [
50 { message: 'First name is required', path: ['users', 0, 'name'] },
51 { message: 'Second name is required', path: ['users', '1', 'name'] },
52 ] satisfies Array<StandardSchemaV1Issue>
53β
54 expect(
55 prefixSchemaToErrors(issues, {
56 users: [{ name: '' }, { name: '' }],
57 }),
58 ).toEqual({
59 'users[0].name': [issues[0]],
60 'users[1].name': [issues[1]],
61 })
62 })
63β
64 it('keeps building the path after traversal reaches a primitive value', () => {
65 const issue = {
66 message: 'Length is too short',
67 path: ['name', 'length'],
68 } satisfies StandardSchemaV1Issue
69β
70 expect(prefixSchemaToErrors([issue], { name: '' })).toEqual({
71 'name.length': [issue],
72 })
73 })
74β
75 it('skips undefined path segments', () => {
76 const issue = {
77 message: 'Value is required',
78 path: ['user', undefined, 'name'],
79 } as StandardSchemaV1Issue
80β
81 expect(prefixSchemaToErrors([issue], { user: { name: '' } })).toEqual({
82 'user.name': [issue],
83 })
84 })
85β
86 it('skips empty string path segments as no accessor', () => {
87 const issues = [
88 { message: 'Middle segment', path: ['foo', '', 'bar'] },
89 { message: 'Leading segment', path: ['', 'bar'] },
90 { message: 'Object segment', path: ['foo', { key: '' }, 'baz'] },
91 ] satisfies Array<StandardSchemaV1Issue>
92β
93 expect(
94 prefixSchemaToErrors(issues, {
95 foo: { bar: '', baz: '' },
96 bar: '',
97 }),
98 ).toEqual({
99 'foo.bar': [issues[0]],
100 bar: [issues[1]],
101 'foo.baz': [issues[2]],
102 })
103 })
104β
105 it('does not treat empty string path segments as array indexes', () => {
106 const issue = {
107 message: 'Name is required',
108 path: ['users', '', 'name'],
109 } satisfies StandardSchemaV1Issue
110β
111 expect(prefixSchemaToErrors([issue], { users: [{ name: '' }] })).toEqual({
112 'users.name': [issue],
113 })
114 })
115 })
116β
117 describe('isStandardSchema', () => {
118 it('identifies standard schema objects', () => {
119 expect(isStandardSchema(getSchema((value) => ({ value })))).toBe(true)
120 })
121β
122 it('rejects validator functions and plain objects', () => {
123 expect(isStandardSchema(() => null)).toBe(false)
124 expect(
125 isStandardSchema({ run: () => null } as unknown as StandardSchemaV1),
126 ).toBe(false)
127 })
128 })
129β
130 describe('parseStandardSchema', () => {
131 it('returns branded schema output for successful validation', async () => {
132 const schema = getSchema((value) => ({
133 value: { parsed: String(value).toUpperCase() },
134 }))
135β
136 const result = await parseStandardSchema(schema, 'value', 'form')
137β
138 expect(result).toEqual({
139 [VALIDATION_OUTPUT]: { parsed: 'VALUE' },
140 })
141 })
142β
143 it('returns field issues directly for field scope', async () => {
144 const issues = [
145 { message: 'Field is required', path: ['name'] },
146 ] satisfies Array<StandardSchemaV1Issue>
147 const schema = getSchema(() => ({ issues }))
148β
149 const result = await parseStandardSchema(schema, '', 'field')
150β
151 expect(result).toEqual(issues)
152 })
153β
154 it('returns prefixed field errors for form scope', async () => {
155 const issues = [
156 { message: 'Name is required', path: ['users', 0, 'name'] },
157 ] satisfies Array<StandardSchemaV1Issue>
158 const schema = getSchema(() => Promise.resolve({ issues }))
159β
160 const result = await parseStandardSchema(
161 schema,
162 { users: [{ name: '' }] },
163 'form',
164 )
165β
166 expect(result).toEqual({
167 form: issues,
168 fields: {
169 'users[0].name': issues,
170 },
171 })
172 })
173β
174 it('returns all schema issues as form-level errors and filters root issues from fields', async () => {
175 const issues = [
176 { message: 'Invalid form' },
177 { message: 'Empty string paths are root errors', path: [''] },
178 { message: 'Name is required', path: ['name'] },
179 ] satisfies Array<StandardSchemaV1Issue>
180 const schema = getSchema(() => ({ issues }))
181β
182 const result = await parseStandardSchema(schema, { name: '' }, 'form')
183β
184 expect(result).toEqual({
185 form: issues,
186 fields: {
187 name: [issues[2]],
188 },
189 })
190 })
191β
192 it('calls validate with the provided value', async () => {
193 const validate = vi.fn((value: unknown) => ({ value }))
194 const schema = getSchema(validate)
195 const value = { name: 'Test' }
196β
197 await parseStandardSchema(schema, value, 'form')
198β
199 expect(validate).toHaveBeenCalledWith(value)
200 })
201 })
202})