Skip to content

Commit 1eace68

Browse files
committed
Adapter fixed, but tests fail due to not all errors being reported.
1 parent 1488627 commit 1eace68

File tree

4 files changed

+48
-38
lines changed

4 files changed

+48
-38
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@
106106
"@sinclair/typebox": ">=0.32.27 <1",
107107
"@sveltejs/kit": "1.x || 2.x",
108108
"@vinejs/vine": "^1.7.1",
109-
"arktype": "1.0.29-alpha",
109+
"arktype": ">=2.0.0-dev.11",
110110
"joi": "^17.13.1",
111111
"superstruct": "^1.0.3",
112112
"svelte": "3.x || 4.x || >=5.0.0-next.51",
@@ -149,7 +149,7 @@
149149
"@sinclair/typebox": "^0.32.27",
150150
"@sodaru/yup-to-json-schema": "^2.0.1",
151151
"@vinejs/vine": "^1.8.0",
152-
"arktype": "1.0.29-alpha",
152+
"arktype": "2.0.0-dev.11",
153153
"joi": "^17.13.1",
154154
"json-schema-to-ts": "^3.0.1",
155155
"superstruct": "^1.0.4",

pnpm-lock.yaml

Lines changed: 22 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/adapters/arktype.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Type } from 'arktype';
1+
import { type, type Type } from 'arktype';
22
import {
33
type ValidationAdapter,
44
type RequiredDefaultsOptions,
@@ -11,22 +11,23 @@ import {
1111
} from './adapters.js';
1212
import { memoize } from '$lib/memoize.js';
1313

14-
async function validate<T extends Type>(
14+
async function _validate<T extends Type>(
1515
schema: T,
1616
data: unknown
1717
): Promise<ValidationResult<Infer<T>>> {
1818
const result = schema(data);
19-
if (result.problems == null) {
19+
if (!(result instanceof type.errors)) {
2020
return {
21-
data: result.data as Infer<T>,
21+
data: result as Infer<T>,
2222
success: true
2323
};
2424
}
25+
const issues = [];
26+
for (const error of result) {
27+
issues.push({ message: error.message, path: error.path });
28+
}
2529
return {
26-
issues: Array.from(result.problems).map(({ message, path }) => ({
27-
message,
28-
path
29-
})),
30+
issues,
3031
success: false
3132
};
3233
}
@@ -39,29 +40,14 @@ function _arktype<T extends Type>(
3940
superFormValidationLibrary: 'arktype',
4041
defaults: options.defaults,
4142
jsonSchema: createJsonSchema(options),
42-
async validate(data) {
43-
const result = schema(data);
44-
if (result.problems == null) {
45-
return {
46-
data: result.data as Infer<T>,
47-
success: true
48-
};
49-
}
50-
return {
51-
issues: Array.from(result.problems).map(({ message, path }) => ({
52-
message,
53-
path
54-
})),
55-
success: false
56-
};
57-
}
43+
validate: async (data) => _validate(schema, data)
5844
});
5945
}
6046

6147
function _arktypeClient<T extends Type>(schema: T): ClientValidationAdapter<Infer<T>, InferIn<T>> {
6248
return {
6349
superFormValidationLibrary: 'arktype',
64-
validate: async (data) => validate(schema, data)
50+
validate: async (data) => _validate(schema, data)
6551
};
6652
}
6753

src/tests/superValidate.test.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ describe('Schemasafe', () => {
310310

311311
/////////////////////////////////////////////////////////////////////
312312

313-
describe('Arktype', () => {
313+
describe.only('Arktype', () => {
314314
const schema = type({
315315
name: 'string',
316316
email: 'email',
@@ -723,15 +723,22 @@ function schemaTest(
723723
) {
724724
const validD = { ...validData, date: dateFormat !== 'Date' ? '2024-01-01' : validData.date };
725725

726+
function missingError(error: string) {
727+
return `Validation error "${error}" not found`;
728+
}
729+
726730
// eslint-disable-next-line @typescript-eslint/no-explicit-any
727731
function expectErrors(errors: ErrorFields, errorMessages: Record<string, any>) {
728732
// console.log('🚀 ~ expectErrors ~ errorMessages:', errorMessages);
729733

730-
if (errors.includes('nospace')) expect(errorMessages.nospace).toBeTruthy();
731-
if (errors.includes('email')) expect(errorMessages.email).toBeTruthy();
732-
if (errors.includes('date')) expect(errorMessages.date).toBeTruthy();
733-
if (errors.includes('tags')) expect(errorMessages?.tags?._errors?.[0]).toBeTruthy();
734-
if (errors.includes('tags[1]')) expect(errorMessages?.tags?.['1']?.[0]).toBeTruthy();
734+
if (errors.includes('nospace'))
735+
expect(errorMessages.nospace, missingError('nospace')).toBeTruthy();
736+
if (errors.includes('email')) expect(errorMessages.email, missingError('email')).toBeTruthy();
737+
if (errors.includes('date')) expect(errorMessages.date, missingError('date')).toBeTruthy();
738+
if (errors.includes('tags'))
739+
expect(errorMessages?.tags?._errors?.[0], missingError('tags')).toBeTruthy();
740+
if (errors.includes('tags[1]'))
741+
expect(errorMessages?.tags?.['1']?.[0], missingError('tags[1]')).toBeTruthy();
735742

736743
const errorCount = errors.filter((path) => traversePath(errorMessages, splitPath(path))?.value);
737744

0 commit comments

Comments
 (0)