mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-03-12 19:37:35 +03:00
61 lines
1.4 KiB
TypeScript
61 lines
1.4 KiB
TypeScript
// Utilities for handling anyOf with null patterns
|
|
import type { StrictRJSFSchema } from "@rjsf/utils";
|
|
|
|
/**
|
|
* Checks if a schema is anyOf/oneOf with exactly [Type, null].
|
|
* This indicates a nullable field in Pydantic schemas.
|
|
*/
|
|
export function isNullableUnionSchema(schema: StrictRJSFSchema): boolean {
|
|
const union = schema.anyOf ?? schema.oneOf;
|
|
if (!union || !Array.isArray(union) || union.length !== 2) {
|
|
return false;
|
|
}
|
|
|
|
let hasNull = false;
|
|
let nonNullCount = 0;
|
|
|
|
for (const item of union) {
|
|
if (typeof item !== "object" || item === null) {
|
|
return false;
|
|
}
|
|
|
|
const itemSchema = item as StrictRJSFSchema;
|
|
|
|
if (itemSchema.type === "null") {
|
|
hasNull = true;
|
|
} else {
|
|
nonNullCount += 1;
|
|
}
|
|
}
|
|
|
|
return hasNull && nonNullCount === 1;
|
|
}
|
|
|
|
/**
|
|
* Backwards-compatible alias for nullable fields
|
|
*/
|
|
export function isSimpleNullableField(schema: StrictRJSFSchema): boolean {
|
|
return isNullableUnionSchema(schema);
|
|
}
|
|
|
|
/**
|
|
* Get the non-null schema from an anyOf containing [Type, null]
|
|
*/
|
|
export function getNonNullSchema(
|
|
schema: StrictRJSFSchema,
|
|
): StrictRJSFSchema | null {
|
|
const union = schema.anyOf ?? schema.oneOf;
|
|
if (!union || !Array.isArray(union)) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
(union.find(
|
|
(item) =>
|
|
typeof item === "object" &&
|
|
item !== null &&
|
|
(item as StrictRJSFSchema).type !== "null",
|
|
) as StrictRJSFSchema) || null
|
|
);
|
|
}
|