140 lines
4.5 KiB
TypeScript
140 lines
4.5 KiB
TypeScript
import { describe, expect, it } from "@suaveplan/testing/runner";
|
|
import { findArrayLengthErrors } from "./find-array-length-errors.js";
|
|
|
|
describe("findArrayLengthErrors", () => {
|
|
it("returns empty array for null schema", () => {
|
|
expect(findArrayLengthErrors(null, [1, 2, 3])).toEqual([]);
|
|
});
|
|
|
|
it("returns empty array for null data", () => {
|
|
expect(findArrayLengthErrors([1, 2], null)).toEqual([]);
|
|
});
|
|
|
|
it("returns empty array for undefined data", () => {
|
|
expect(findArrayLengthErrors([1, 2], undefined)).toEqual([]);
|
|
});
|
|
|
|
it("handles optional wrapper with undefined data", () => {
|
|
const schema = { _bp: { schema: 255 } };
|
|
expect(
|
|
findArrayLengthErrors(
|
|
schema as unknown as import("@suaveplan/types").BitPackerSchema,
|
|
undefined
|
|
)
|
|
).toEqual([]);
|
|
});
|
|
|
|
it("unwraps optional schema and checks inner", () => {
|
|
const schema = { _bp: { schema: [1, 2] } };
|
|
const data = [1, 2, 3];
|
|
const errors = findArrayLengthErrors(
|
|
schema as unknown as import("@suaveplan/types").BitPackerSchema,
|
|
data
|
|
);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0].expected).toBe(2);
|
|
expect(errors[0].actual).toBe(3);
|
|
});
|
|
|
|
it("detects array length exceeded", () => {
|
|
const schema = [1, 2];
|
|
const data = [1, 2, 3];
|
|
const errors = findArrayLengthErrors(schema, data);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toMatchObject({
|
|
path: "",
|
|
expected: 2,
|
|
actual: 3,
|
|
});
|
|
});
|
|
|
|
it("checks nested array elements", () => {
|
|
const schema = [[1], [2]];
|
|
const data = [
|
|
[1, 2],
|
|
[2, 3, 4],
|
|
];
|
|
const errors = findArrayLengthErrors(schema, data);
|
|
expect(errors.length).toBe(2);
|
|
expect(errors[0]).toMatchObject({
|
|
path: "[0]",
|
|
expected: 1,
|
|
actual: 2,
|
|
});
|
|
expect(errors[1]).toMatchObject({
|
|
path: "[1]",
|
|
expected: 1,
|
|
actual: 3,
|
|
});
|
|
});
|
|
|
|
it("handles array within object", () => {
|
|
const schema = { items: [1, 2] };
|
|
const data = { items: [1, 2, 3] };
|
|
const errors = findArrayLengthErrors(schema, data);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toMatchObject({
|
|
path: "items",
|
|
expected: 2,
|
|
actual: 3,
|
|
});
|
|
});
|
|
|
|
it("handles object schema with nested arrays", () => {
|
|
const schema = { a: { b: [1, 2] } };
|
|
const data = { a: { b: [1, 2, 3, 4] } };
|
|
const errors = findArrayLengthErrors(schema, data);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toMatchObject({
|
|
path: "a.b",
|
|
expected: 2,
|
|
actual: 4,
|
|
});
|
|
});
|
|
|
|
it("skips undefined properties in object schema", () => {
|
|
const schema = { a: [1], b: [1] };
|
|
const data = { a: [1, 2] };
|
|
const errors = findArrayLengthErrors(schema, data);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0].path).toBe("a");
|
|
});
|
|
|
|
it("handles array config with maxLength exceeded", () => {
|
|
const schema = { type: "array", maxLength: 3 };
|
|
const data = [1, 2, 3, 4];
|
|
const errors = findArrayLengthErrors(schema, data);
|
|
expect(errors.length).toBe(1);
|
|
expect(errors[0]).toMatchObject({ expected: 3, actual: 4 });
|
|
});
|
|
|
|
it("returns empty when array config maxLength is not exceeded", () => {
|
|
const schema = { type: "array", maxLength: 5 };
|
|
const data = [1, 2, 3];
|
|
expect(findArrayLengthErrors(schema, data)).toEqual([]);
|
|
});
|
|
|
|
it("returns empty for valid array length", () => {
|
|
const schema = [1, 2, 3];
|
|
const data = [1, 2];
|
|
expect(findArrayLengthErrors(schema, data)).toEqual([]);
|
|
});
|
|
|
|
it("returns empty for non-array data with array schema", () => {
|
|
const schema = [1, 2];
|
|
const data = "not an array";
|
|
expect(findArrayLengthErrors(schema, data)).toEqual([]);
|
|
});
|
|
|
|
it("returns empty for primitive schema", () => {
|
|
expect(findArrayLengthErrors(255, 100)).toEqual([]);
|
|
});
|
|
|
|
it("skips inherited properties in schema", () => {
|
|
const schema = Object.create({ inherited: [1, 2, 3] });
|
|
schema.a = 255;
|
|
const data = { a: 1 };
|
|
expect(findArrayLengthErrors(schema, data)).toEqual([]);
|
|
});
|
|
});
|