Skip to content

feat: Add typia adapter #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
engine-strict=true
@jsr:registry=https://npm.jsr.io
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -53,7 +53,8 @@
"check:adapters": "node types-exist.js",
"test": "vitest run",
"lint": "prettier --check . && eslint .",
"format": "prettier --write ."
"format": "prettier --write .",
"prepare": "ts-patch install && typia patch"
},
"exports": {
".": {
@@ -141,6 +142,9 @@
},
"@vinejs/vine": {
"optional": true
},
"typia": {
"optional": true
}
},
"optionalDependencies": {
@@ -165,6 +169,7 @@
"ts-deepmerge": "^7.0.0"
},
"devDependencies": {
"@ryoppippi/unplugin-typia": "npm:@jsr/ryoppippi__unplugin-typia@^0.3.7",
"@sveltejs/adapter-auto": "^3.2.1",
"@sveltejs/kit": "^2.5.10",
"@sveltejs/package": "^2.3.1",
@@ -190,8 +195,11 @@
"sveltekit-flash-message": "^2.4.4",
"sveltekit-rate-limiter": "^0.5.1",
"throttle-debounce": "^5.0.0",
"ts-node": "^10.9.2",
"ts-patch": "^3.2.0",
"tslib": "^2.6.2",
"typescript": "^5.4.5",
"typia": "^6.0.6",
"uuid": "^9.0.1",
"vite": "^5.2.11",
"vitest": "^1.6.0"
1,131 changes: 1,122 additions & 9 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/lib/adapters/adapters.ts
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@ export type ValidationLibrary =
| 'joi'
| 'superform'
| 'typebox'
| 'typia'
| 'valibot'
| 'yup'
| 'zod'
65 changes: 65 additions & 0 deletions src/lib/adapters/typia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import type { IValidation } from 'typia';
import {
type ValidationAdapter,
type ValidationIssue,
type RequiredDefaultsOptions,
createAdapter,
type ClientValidationAdapter,
type ValidationResult,
createJsonSchema
} from './adapters.js';
import { memoize } from '$lib/memoize.js';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Validation<O = any> = (input: unknown) => IValidation<O>;
type OutputType<T> = T extends Validation<infer O> ? O : never;

async function _validate<T extends Validation>(
validate: T,
data: unknown
): Promise<ValidationResult<OutputType<T>>> {
const result = validate(data);
if (result.success) {
return {
data: result.data,
success: true
};
}

const issues: ValidationIssue[] = [];
for (const { expected, value, path } of result.errors) {
issues.push({
path: [path.replace('$input.', '')],
message: JSON.stringify({ expected, value })
});
}

return {
issues,
success: false
};
}

function _typia<T extends Validation>(
validate: T,
options: RequiredDefaultsOptions<OutputType<T>>
): ValidationAdapter<OutputType<T>> {
return createAdapter({
superFormValidationLibrary: 'typia',
defaults: options.defaults,
jsonSchema: createJsonSchema(options),
validate: async (data) => _validate(validate, data)
});
}

function _typiaClient<T extends Validation>(
validate: T
): ClientValidationAdapter<ReturnType<typeof _validate<T>>> {
return {
superFormValidationLibrary: 'typia',
validate: async (data) => _validate(validate, data)
};
}

export const typia = /* @__PURE__ */ memoize(_typia);
export const typiaClient = /* @__PURE__ */ memoize(_typiaClient);
21 changes: 21 additions & 0 deletions src/tests/superValidate.test.ts
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ import Vine from '@vinejs/vine';

import { schemasafe } from '$lib/adapters/schemasafe.js';

import { typia as typiaAdapter } from '$lib/adapters/typia.js';
import typia, { type tags } from 'typia';

import { traversePath } from '$lib/traversal.js';
import { splitPath } from '$lib/stringPath.js';
import { SchemaError, type JSONSchema } from '$lib/index.js';
@@ -457,6 +460,24 @@ describe('ajv', () => {

/////////////////////////////////////////////////////////////////////

describe('typia', () => {
interface Schema extends Record<string, unknown> {
name: string;
email: string & tags.Format<'email'>;
tags: (string & tags.MinLength<2>)[] & tags.MinItems<3>;
score: number & tags.Type<'uint32'> & tags.Minimum<0>;
date?: Date;
nospace?: string & tags.Pattern<'^\\S*$'>;
extra: string | null;
}

const validate = typia.createValidate<Schema>();
const adapter = typiaAdapter(validate, { defaults });
schemaTest(adapter, ['email', 'nospace', 'tags'], 'simple');
});

/////////////////////////////////////////////////////////////////////

describe('Zod', () => {
const schema = z
.object({
3 changes: 2 additions & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vitest/config';
import UnpluginTypia from '@ryoppippi/unplugin-typia/vite';

export default defineConfig({
plugins: [sveltekit()],
plugins: [UnpluginTypia(), sveltekit()],
test: {
include: ['src/**/*.{test,spec}.{js,ts}']
},