Skip to content

Commit 5213b64

Browse files
author
Ruben van Leeuwen
committed
2046: Improve checking for invalid combinator situations
1 parent 4694915 commit 5213b64

File tree

1 file changed

+12
-12
lines changed
  • frontend/packages/pydantic-forms/src/core

1 file changed

+12
-12
lines changed

frontend/packages/pydantic-forms/src/core/helper.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ export const flattenSchemaCombinators = (
9393
}
9494

9595
if (
96-
(isNullable(propertySchemaParsed) &&
96+
(containsNullableSchema(propertySchemaParsed.oneOf) &&
9797
propertySchemaParsed.oneOf.length > 2) ||
98-
(!isNullable(propertySchemaParsed) &&
98+
(!containsNullableSchema(propertySchemaParsed.oneOf) &&
9999
propertySchemaParsed.oneOf.length > 1)
100100
) {
101101
console.warn(
@@ -111,9 +111,9 @@ export const flattenSchemaCombinators = (
111111

112112
if (propertySchemaParsed.anyOf) {
113113
if (
114-
(isNullable(propertySchemaParsed) &&
114+
(containsNullableSchema(propertySchemaParsed.anyOf) &&
115115
propertySchemaParsed.anyOf.length > 2) ||
116-
(!isNullable(propertySchemaParsed) &&
116+
(!containsNullableSchema(propertySchemaParsed.anyOf) &&
117117
propertySchemaParsed.anyOf.length > 1)
118118
) {
119119
console.warn(
@@ -208,23 +208,23 @@ export const getFlatFieldMap = (
208208
* Checks if the schema's type or one of the combinator props anyOf or oneOf contains a type of 'null'.
209209
* This tells us if the field is allowed to be null or not.
210210
*
211-
* @param schema A field from the 'properties' key of the JSON Schema
212211
* @returns true if the schema is nullable, false otherwise
213212
*/
214213
export const isNullable = (schema: PydanticFormPropertySchemaParsed) => {
215214
// Check if the schema has a type of 'null' or if it has an anyOf with a type of 'null'
216215
const isNullType = schema.type === PydanticFormFieldType.NULL;
217-
const hasNullAnyOf = schema.anyOf?.some(
218-
(item) => item.type === PydanticFormFieldType.NULL,
219-
);
220-
221-
const hasNullOneOf = schema.oneOf?.some(
222-
(item) => item.type === PydanticFormFieldType.NULL,
223-
);
216+
const hasNullAnyOf = containsNullableSchema(schema.anyOf ?? []);
217+
const hasNullOneOf = containsNullableSchema(schema.oneOf ?? []);
224218

225219
return isNullType || hasNullAnyOf || hasNullOneOf || false;
226220
};
227221

222+
const containsNullableSchema = (
223+
schemas: PydanticFormPropertySchemaParsed[],
224+
): boolean => {
225+
return schemas.some((item) => item.type === PydanticFormFieldType.NULL);
226+
};
227+
228228
/**
229229
* Field to validation object
230230
*

0 commit comments

Comments
 (0)