-
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces a new validation adapter for the 'typia' library. The adapter is implemented in 'src/lib/adapters/typia.ts'. It includes functions for both server-side and client-side validation. The implementation also includes a test case in 'src/tests/superValidate.test.ts' to ensure the adapter works as expected.
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters