Skip to content

Commit 4f25224

Browse files
committed
Fixed dynamic data for schemasafe adapter
1 parent 78ca8f9 commit 4f25224

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/lib/adapters/schemasafe.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ const Email =
2020

2121
// Type inference problem unless this is applied: https://github.com/ThomasAribart/json-schema-to-ts/blob/main/documentation/FAQs/applying-from-schema-on-generics.md
2222

23-
function _schemasafe<T extends JSONSchema, Data = FromSchema<T>>(
23+
function _schemasafe<
24+
T extends JSONSchema | Record<string, unknown>,
25+
Data = unknown extends FromSchema<T> ? Record<string, unknown> : FromSchema<T>
26+
>(
2427
schema: T,
2528
options?: AdapterOptions<Data> & { config?: ValidatorOptions }
2629
): ValidationAdapter<Data> {

src/tests/superValidate.test.ts

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ describe('TypeBox', () => {
186186
});
187187

188188
describe('Schemasafe', () => {
189-
const schema = {
189+
const constSchema = {
190190
$id: 'https://example.com/user-data',
191191
$schema: 'http://json-schema.org/draft-07/schema',
192192
type: 'object',
@@ -222,12 +222,57 @@ describe('Schemasafe', () => {
222222
additionalProperties: false
223223
} as const;
224224

225+
const schema = {
226+
$id: 'https://example.com/user-data',
227+
$schema: 'http://json-schema.org/draft-07/schema',
228+
type: 'object',
229+
properties: {
230+
name: { type: 'string', default: 'Unknown' },
231+
email: {
232+
type: 'string',
233+
format: 'email'
234+
},
235+
tags: {
236+
type: 'array',
237+
minItems: 3,
238+
items: { type: 'string', minLength: 2 }
239+
},
240+
score: { type: 'integer', minimum: 0 },
241+
date: { type: 'string', format: 'date' },
242+
nospace: {
243+
pattern: '^\\S*$',
244+
type: 'string'
245+
},
246+
extra: {
247+
anyOf: [
248+
{
249+
type: 'string'
250+
},
251+
{
252+
type: 'null'
253+
}
254+
]
255+
}
256+
},
257+
required: ['name', 'email', 'tags', 'score', 'extra'],
258+
additionalProperties: false
259+
};
260+
225261
// Type test
226-
const adapter = schemasafe(schema, { defaults });
227-
const a: number = adapter.defaults.score;
262+
const constAdapter = schemasafe(constSchema);
263+
const a: number = constAdapter.defaults.score;
228264
a;
229265

230-
schemaTest(adapter, ['email', 'nospace', 'tags', 'tags[1]'], 'full', 'string');
266+
const adapter = schemasafe(schema, { defaults });
267+
const b: number = adapter.defaults.score;
268+
b;
269+
270+
const dynamicAdapter = schemasafe(schema);
271+
const c: Record<string, unknown> = dynamicAdapter.defaults;
272+
c;
273+
274+
schemaTest(constAdapter, undefined, 'full', 'string');
275+
schemaTest(adapter, undefined, 'full', 'string');
231276
});
232277

233278
/////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)