Skip to content

Commit c30363a

Browse files
committed
Arktype is full adapter now!
1 parent 83bffa7 commit c30363a

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- The [Arktype](https://arktype.io/) adapter is finally a "full" adapter, meaning it's retrospectable and doesn't require default values anymore!
13+
1014
### Fixed
1115

1216
- SuperDebug rune version is back, can now be imported as `import SuperDebug from 'sveltekit-superforms/SuperDebug.svelte';`

src/lib/adapters/arktype.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,16 @@ const fetchModule = /* @__PURE__ */ memoize(modules);
2121

2222
const defaultJSONSchemaOptions = {
2323
fallback: {
24-
default: (ctx) => ctx.base,
24+
default: (ctx) => {
25+
if ('domain' in ctx && ctx.domain === 'bigint') {
26+
return {
27+
...ctx.base,
28+
type: 'string',
29+
format: 'bigint'
30+
};
31+
}
32+
return ctx.base;
33+
},
2534
date: (ctx) => ({
2635
...ctx.base,
2736
type: 'string',

src/tests/superValidate.test.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const defaults = {
144144
/**
145145
* Expected constraints for libraries with introspection
146146
*/
147-
function fullConstraints(library: 'zod4' | 'unknown') {
147+
function fullConstraints(library: 'zod4' | 'arktype' | 'unknown') {
148148
const defaultConstraints = {
149149
email: {
150150
required: true
@@ -176,6 +176,14 @@ function fullConstraints(library: 'zod4' | 'unknown') {
176176
max: Number.MAX_SAFE_INTEGER
177177
}
178178
};
179+
case 'arktype':
180+
return {
181+
...defaultConstraints,
182+
email: {
183+
...defaultConstraints.email,
184+
pattern: '^[\\w%+.-]+@[\\d.A-Za-z-]+\\.[A-Za-z]{2,}$'
185+
}
186+
};
179187
default:
180188
return defaultConstraints;
181189
}
@@ -368,18 +376,39 @@ describe('Schemasafe', () => {
368376
/////////////////////////////////////////////////////////////////////
369377

370378
describe('Arktype', () => {
379+
it('should handle defaults, even when upgraded to full adapter', async () => {
380+
const schema = type({
381+
name: 'string > 2'
382+
});
383+
const adapter = arktype(schema, { defaults: { name: 'Test' } });
384+
const form = await superValidate({}, adapter);
385+
expect(form.data).toEqual({ name: 'Test' });
386+
});
387+
388+
it('should handle bigint', async () => {
389+
const schema = type({
390+
id: 'bigint'
391+
});
392+
const adapter = arktype(schema);
393+
const formData = new FormData();
394+
formData.set('id', '123456789123456789');
395+
396+
const form = await superValidate(formData, adapter);
397+
expect(form.data).toEqual({ id: 123456789123456789n });
398+
});
399+
371400
const schema = type({
372-
name: 'string',
401+
name: 'string = "Unknown"',
373402
email: 'string.email',
374403
tags: '(string>=2)[]>=3',
375404
score: 'number.integer>=0',
376-
'date?': 'Date',
405+
'date?': 'Date | undefined',
377406
'nospace?': nospacePattern,
378407
extra: 'string|null'
379408
});
380409

381-
const adapter = arktype(schema, { defaults });
382-
schemaTest(adapter, ['email', 'date', 'nospace', 'tags'], 'simple');
410+
const adapter = arktype(schema);
411+
schemaTest(adapter, ['email', 'nospace', 'tags'], undefined, undefined, 'arktype');
383412
});
384413

385414
/////////////////////////////////////////////////////////////////////
@@ -1302,7 +1331,7 @@ function schemaTest(
13021331
errors: ErrorFields = ['email', 'nospace', 'tags', 'tags[1]'],
13031332
adapterType: 'full' | 'simple' = 'full',
13041333
dateFormat: 'Date' | 'string' | 'stringToDate' = 'Date',
1305-
library: 'unknown' | 'zod4' = 'unknown'
1334+
library: 'unknown' | 'zod4' | 'arktype' = 'unknown'
13061335
) {
13071336
const validD = { ...validData, date: dateFormat !== 'Date' ? '2024-01-01' : validData.date };
13081337

@@ -1312,8 +1341,6 @@ function schemaTest(
13121341

13131342
// eslint-disable-next-line @typescript-eslint/no-explicit-any
13141343
function expectErrors(errors: ErrorFields, errorMessages: Record<string, any>) {
1315-
// console.log('🚀 ~ expectErrors ~ errorMessages:', errorMessages);
1316-
13171344
if (errors.includes('nospace'))
13181345
expect(errorMessages.nospace, missingError('nospace')).toBeTruthy();
13191346
if (errors.includes('email')) expect(errorMessages.email, missingError('email')).toBeTruthy();

0 commit comments

Comments
 (0)