Skip to content
Open
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions src/lib/formData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,8 @@ function _parseFormData<T extends Record<string, unknown>>(
} else {
let unionKeys: string[] = [];

// Special fix for union schemas, then the keys must be gathered from the objects in the union
if (schema.anyOf) {
// Special fix for (discriminated) union schemas, then the keys must be gathered from the objects in the union
if (schema.anyOf || schema.oneOf) {
const info = schemaInfo(schema, false, []);
if (info.union?.some((s) => s.type !== 'object')) {
throw new SchemaError('All form types must be an object if schema is a union.');
Expand Down
15 changes: 12 additions & 3 deletions src/lib/jsonSchema/schemaInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const conversionFormatTypes = [
];

/**
* Normalizes the different kind of schema variations (anyOf, union, const null, etc)
* Normalizes the different kind of schema variations (anyOf, oneOf, union, const null, etc)
* to figure out the field type, optional, nullable, etc.
*/
export function schemaInfo(
Expand Down Expand Up @@ -121,6 +121,9 @@ function schemaTypes(
if (schema.anyOf) {
types = schema.anyOf.flatMap((s) => schemaTypes(s, path));
}
if (schema.oneOf) {
types = schema.oneOf.flatMap((s) => schemaTypes(s, path));
}

if (types.includes('array') && schema.uniqueItems) {
const i = types.findIndex((t) => t === 'array');
Expand Down Expand Up @@ -158,6 +161,12 @@ function schemaTypes(
}

function unionInfo(schema: JSONSchema7) {
if (!schema.anyOf || !schema.anyOf.length) return undefined;
return schema.anyOf.filter((s) => typeof s !== 'boolean') as JSONSchema7[];
if (!schema.oneOf && !schema.anyOf) return undefined;
if (schema.oneOf && schema.oneOf.length) {
return schema.oneOf.filter((s) => typeof s !== 'boolean') as JSONSchema7[];
}
if (schema.anyOf && schema.anyOf.length) {
return schema.anyOf.filter((s) => typeof s !== 'boolean') as JSONSchema7[];
}
return undefined;
}