Skip to content

Commit 50e2170

Browse files
committed
Loading schemasafe dynamically
1 parent e52fa88 commit 50e2170

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/lib/adapters/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ export { yup, yupClient } from './yup.js';
99
export { zod, zodClient } from './zod.js';
1010
export { vine, vineClient } from './vine.js';
1111
export { schemasafe, schemasafeClient } from './schemasafe.js';
12-
13-
/*
14-
// Cannot use due to moduleResolution problem: https://github.com/ianstormtaylor/superstruct/issues/1200
1512
export { superstruct, superstructClient } from './superstruct.js';
1613

17-
// Cannot use due to not being ESM compatible.
14+
/*
15+
// Cannot use ajv due to not being ESM compatible.
1816
export { ajv } from './ajv.js';
1917
*/

src/lib/adapters/schemasafe.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ import {
55
type ClientValidationAdapter,
66
type ValidationAdapter
77
} from './adapters.js';
8-
import {
9-
validator,
10-
type Json,
11-
type Schema,
12-
type Validate,
13-
type ValidatorOptions
14-
} from '@exodus/schemasafe';
8+
import type { Json, Schema, Validate, ValidatorOptions } from '@exodus/schemasafe';
159
import type { FromSchema, JSONSchema } from 'json-schema-to-ts';
1610
import type { JSONSchema as JSONSchema7 } from '$lib/jsonSchema/index.js';
1711

12+
async function modules() {
13+
const { validator } = await import(/* webpackIgnore: true */ '@exodus/schemasafe');
14+
return { validator };
15+
}
16+
17+
const fetchModule = /* @__PURE__ */ memoize(modules);
18+
1819
/*
1920
* Adapter specificts:
2021
* Type inference problem unless this is applied:
@@ -33,7 +34,9 @@ const defaultOptions = {
3334
allErrors: true
3435
};
3536

36-
function cachedValidator(currentSchema: JSONSchema7, config?: ValidatorOptions) {
37+
async function cachedValidator(currentSchema: JSONSchema7, config?: ValidatorOptions) {
38+
const { validator } = await fetchModule();
39+
3740
if (!cache.has(currentSchema)) {
3841
cache.set(
3942
currentSchema,
@@ -60,7 +63,7 @@ function _schemasafe<
6063
jsonSchema: schema as JSONSchema7,
6164
defaults: options?.defaults,
6265
async validate(data: unknown) {
63-
const validator = cachedValidator(schema as JSONSchema7, options?.config);
66+
const validator = await cachedValidator(schema as JSONSchema7, options?.config);
6467
const isValid = validator(data as Json);
6568

6669
if (isValid) {
@@ -91,7 +94,7 @@ function _schemasafeClient<
9194
return {
9295
superFormValidationLibrary: 'schemasafe',
9396
async validate(data: unknown) {
94-
const validator = cachedValidator(schema as JSONSchema7, options?.config);
97+
const validator = await cachedValidator(schema as JSONSchema7, options?.config);
9598
const isValid = validator(data as Json);
9699

97100
if (isValid) {

0 commit comments

Comments
 (0)