Remove unused function plus tests

This commit is contained in:
Ola Hungerford
2025-02-28 06:26:04 -07:00
parent 82bbe58a46
commit a1eb343b79
2 changed files with 0 additions and 79 deletions

View File

@@ -1,7 +1,6 @@
import {
generateDefaultValue,
formatFieldLabel,
validateValueAgainstSchema,
} from "../schemaUtils";
import { JsonSchemaType } from "../../components/DynamicJsonForm";
@@ -111,43 +110,3 @@ describe("formatFieldLabel", () => {
expect(formatFieldLabel("")).toBe("");
});
});
describe("validateValueAgainstSchema", () => {
test("validates string type", () => {
expect(validateValueAgainstSchema("test", { type: "string" })).toBe(true);
expect(validateValueAgainstSchema(123, { type: "string" })).toBe(false);
});
test("validates number type", () => {
expect(validateValueAgainstSchema(123, { type: "number" })).toBe(true);
expect(validateValueAgainstSchema("test", { type: "number" })).toBe(false);
});
test("validates integer type", () => {
expect(validateValueAgainstSchema(123, { type: "integer" })).toBe(true);
expect(validateValueAgainstSchema("test", { type: "integer" })).toBe(false);
});
test("validates boolean type", () => {
expect(validateValueAgainstSchema(true, { type: "boolean" })).toBe(true);
expect(validateValueAgainstSchema("test", { type: "boolean" })).toBe(false);
});
test("validates array type", () => {
expect(validateValueAgainstSchema([], { type: "array" })).toBe(true);
expect(validateValueAgainstSchema({}, { type: "array" })).toBe(false);
});
test("validates object type", () => {
expect(validateValueAgainstSchema({}, { type: "object" })).toBe(true);
expect(validateValueAgainstSchema([], { type: "object" })).toBe(false);
expect(validateValueAgainstSchema("test", { type: "object" })).toBe(false);
});
test("returns true for unknown types", () => {
expect(
// @ts-expect-error Testing with invalid type
validateValueAgainstSchema("anything", { type: "unknown" }),
).toBe(true);
});
});

View File

@@ -54,41 +54,3 @@ export function formatFieldLabel(key: string): string {
.replace(/_/g, " ") // Replace underscores with spaces
.replace(/^\w/, (c) => c.toUpperCase()); // Capitalize first letter
}
/**
* Validates if a value conforms to a JSON schema
* @param value The value to validate
* @param schema The JSON schema to validate against
* @returns True if valid, false otherwise
*/
export function validateValueAgainstSchema(
value: JsonValue,
schema: JsonSchemaType,
): boolean {
// Handle undefined values for non-required fields
if (value === undefined && !schema.required) {
return true;
}
// Basic type validation
switch (schema.type) {
case "string":
return typeof value === "string";
case "number":
return typeof value === "number";
case "integer":
return typeof value === "number" && Number.isInteger(value);
case "boolean":
return typeof value === "boolean";
case "array":
return Array.isArray(value);
case "object":
return (
typeof value === "object" && value !== null && !Array.isArray(value)
);
case "null":
return value === null;
default:
return true;
}
}