Skip to content
This repository has been archived by the owner on May 6, 2023. It is now read-only.

Commit

Permalink
Merge pull request #49 from saiichihashimoto/array-items
Browse files Browse the repository at this point in the history
feat(array): use items to build up types
  • Loading branch information
kodiakhq[bot] authored Jun 8, 2022
2 parents 4841614 + 7873d3d commit 8d99909
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 156 deletions.
113 changes: 79 additions & 34 deletions src/array/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,28 @@ import { fields } from "../fields";
import { object } from "../object";
import { mockRule } from "../test-utils";

import { array } from ".";
import { array, items } from ".";

import type { ValidateShape } from "../test-utils";
import type { InferInput, InferOutput } from "../types";
import type { PartialDeep } from "type-fest";

describe("array", () => {
it("builds a sanity config", () =>
expect(array().schema()).toEqual({
expect(array({ of: items() }).schema()).toEqual({
type: "array",
of: [],
validation: expect.any(Function),
}));

it("passes through schema values", () =>
expect(array({ hidden: false }).schema()).toHaveProperty("hidden", false));
expect(array({ of: items(), hidden: false }).schema()).toHaveProperty(
"hidden",
false
));

it("parses into an array", () => {
const type = array();
const type = array({ of: items() });

const value: ValidateShape<InferInput<typeof type>, never[]> = [];
const parsedValue: ValidateShape<
Expand All @@ -35,7 +39,7 @@ describe("array", () => {
});

it("adds primitive types", () => {
const type = array().of(boolean());
const type = array({ of: items().item(boolean()) });

const schema = type.schema();

Expand All @@ -58,14 +62,16 @@ describe("array", () => {
});

it("adds nonprimitive types", () => {
const type = array().of(
object({
fields: fields().field({
name: "foo",
type: boolean(),
}),
})
);
const type = array({
of: items().item(
object({
fields: fields().field({
name: "foo",
type: boolean(),
}),
})
),
});

const schema = type.schema();

Expand Down Expand Up @@ -95,23 +101,25 @@ describe("array", () => {
});

it("creates union with multiple types", () => {
const type = array()
.of(
object({
fields: fields().field({
name: "foo",
type: boolean(),
}),
})
)
.of(
object({
fields: fields().field({
name: "bar",
type: boolean(),
}),
})
);
const type = array({
of: items()
.item(
object({
fields: fields().field({
name: "foo",
type: boolean(),
}),
})
)
.item(
object({
fields: fields().field({
name: "bar",
type: boolean(),
}),
})
),
});

const schema = type.schema();

Expand Down Expand Up @@ -151,7 +159,7 @@ describe("array", () => {
});

it("sets min", () => {
const type = array({ min: 1 }).of(boolean());
const type = array({ min: 1, of: items().item(boolean()) });

const rule = mockRule();

Expand All @@ -173,7 +181,7 @@ describe("array", () => {
});

it("sets max", () => {
const type = array({ max: 1 }).of(boolean());
const type = array({ max: 1, of: items().item(boolean()) });

const rule = mockRule();

Expand All @@ -195,7 +203,7 @@ describe("array", () => {
});

it("sets length", () => {
const type = array({ length: 1 }).of(boolean());
const type = array({ length: 1, of: items().item(boolean()) });

const rule = mockRule();

Expand All @@ -220,7 +228,7 @@ describe("array", () => {
});

it("sets nonempty", () => {
const type = array({ nonempty: true }).of(boolean());
const type = array({ nonempty: true, of: items().item(boolean()) });

const rule = mockRule();

Expand All @@ -243,4 +251,41 @@ describe("array", () => {
type.parse([]);
}).toThrow(z.ZodError);
});

it("types custom validation", () => {
const type = array({
of: items()
.item(
object({
fields: fields().field({
name: "foo",
type: boolean(),
}),
})
)
.item(
object({
fields: fields().field({
name: "bar",
type: boolean(),
}),
})
),
validation: (Rule) =>
Rule.custom((value) => {
const elements: ValidateShape<
typeof value,
PartialDeep<Array<{ foo: boolean } | { bar: boolean }>>
> = value;

return elements.length > 50 || "Needs to be 50 characters";
}),
});

const rule = mockRule();

type.schema().validation?.(rule);

expect(rule.custom).toHaveBeenCalledWith(expect.any(Function));
});
});
Loading

0 comments on commit 8d99909

Please sign in to comment.