Skip to content

Commit

Permalink
All properties for object unions weren't added to the adapter defaults.
Browse files Browse the repository at this point in the history
Fixes #519
  • Loading branch information
ciscoheat committed Nov 30, 2024
1 parent f2201d1 commit 0289a8b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Headlines: Added, Changed, Deprecated, Removed, Fixed, Security
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed

- All properties for object unions weren't added to the adapter defaults.

## [2.20.1] - 2024-11-15

### Changed
Expand Down
7 changes: 4 additions & 3 deletions src/lib/jsonSchema/schemaDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])
return _multiType.size > 1;
};

let output: Record<string, unknown> = {};
let output: Record<string, unknown> | undefined = undefined;

// Check unions first, so default values can take precedence over nullable and optional
if (!objectDefaults && info.union) {
Expand Down Expand Up @@ -98,6 +98,7 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])

// Objects must have default values to avoid setting undefined properties on nested data
if (info.union.length && info.types[0] == 'object') {
if (output === undefined) output = {};
output =
info.union.length > 1
? merge.withOptions(
Expand Down Expand Up @@ -128,9 +129,9 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])
: _defaultValues(objSchema, !info.required?.includes(key), [...path, key]);

//if (def !== undefined) output[key] = def;
if (output === undefined) output = {};
output[key] = def;
}
return output;
} else if (objectDefaults) {
return objectDefaults;
}
Expand Down Expand Up @@ -159,7 +160,7 @@ function _defaultValues(schema: JSONSchema, isOptional: boolean, path: string[])

const [formatType] = info.types;

return defaultValue(formatType, schema.enum);
return output ?? defaultValue(formatType, schema.enum);
}

function formatDefaultValue(type: SchemaType, value: unknown) {
Expand Down
59 changes: 59 additions & 0 deletions src/tests/zodUnion.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { zod } from '$lib/adapters/zod.js';
import { superValidate } from '$lib/superValidate.js';
import { stringify } from 'devalue';
import { describe, test } from 'vitest';
import { z } from 'zod';

const ZodSchema = z.object({
addresses: z.object({
additional: z.discriminatedUnion('type', [
z.object({
type: z.literal('poBox'),
name: z.string().min(1, 'min len').max(10, 'max len')
}),
z.object({
type: z.literal('none')
})
])
})
});
const FormSchema = zod(ZodSchema);
type FormSchema = (typeof FormSchema)['defaults'];

async function validate(data: unknown) {
const formInput = new FormData();

formInput.set('__superform_json', stringify(data));
try {
await superValidate(formInput, FormSchema);
} catch (err) {
console.error(err);
//
throw err;
}
}

describe('Demo', () => {
test('Bad', async () => {
const data = {
addresses: {
additional: {
type: 'poBox',
name: ''
}
}
} satisfies FormSchema;
await validate(data);
});

test('Good', async () => {
const data = {
addresses: {
additional: {
type: 'none'
}
}
} satisfies FormSchema;
await validate(data);
});
});

0 comments on commit 0289a8b

Please sign in to comment.