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

Commit

Permalink
fix(package): use @sanity-typed/schema-builder for everything
Browse files Browse the repository at this point in the history
  • Loading branch information
saiichihashimoto committed May 5, 2023
1 parent 4144273 commit 82d072b
Show file tree
Hide file tree
Showing 41 changed files with 1,084 additions and 3,559 deletions.
951 changes: 162 additions & 789 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
},
"dependencies": {
"@portabletext/types": "2.0.2",
"@sanity-typed/schema-builder": "2.2.4",
"@types/lodash": "4.14.194",
"lodash": "4.17.21",
"type-fest": "2.19.0",
Expand Down
74 changes: 34 additions & 40 deletions src/array/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
import { describe, expect, it } from "@jest/globals";
import { s } from "@sanity-typed/schema-builder";
import { z } from "zod";

import { array } from ".";
import { boolean } from "../boolean";
import { object } from "../object";
import { objectNamed } from "../objectNamed";
import { string } from "../string";
import { mockRule } from "../test-utils";
import type { Equal, Expect } from "../test-utils";
import type { InferValue } from "../types";

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

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

it("adds primitive types", () => {
const type = array({
const type = s.array({
of: [
boolean({
s.boolean({
zod: (zod) => zod.transform((value) => value.toString()),
zodResolved: (zod) =>
zod.transform((value) => value.toString().length),
}),
],
});

const value = [true] as InferValue<typeof type>;
const value = [true] as s.infer<typeof type>;
const parsedValue = type.parse(value);
const resolvedValue = type.resolve(value);

Expand All @@ -50,13 +44,13 @@ describe("array", () => {
});

it("adds keyed nonprimitive types", () => {
const type = array({
const type = s.array({
of: [
object({
s.object({
fields: [
{
name: "foo",
type: boolean({
type: s.boolean({
zod: (zod) => zod.transform((value) => value.toString()),
zodResolved: (zod) =>
zod.transform((value) => value.toString().length),
Expand Down Expand Up @@ -85,7 +79,7 @@ describe("array", () => {
const value = [
{ _key: "a", foo: true },
{ _key: "b", foo: false },
] as InferValue<typeof type>;
] as s.infer<typeof type>;
const parsedValue = type.parse(value);
const resolvedValue = type.resolve(value);

Expand All @@ -106,8 +100,8 @@ describe("array", () => {
});

it("creates union with primitive types", () => {
const type = array({
of: [boolean(), string()],
const type = s.array({
of: [s.boolean(), s.string()],
});

const schema = type.schema();
Expand All @@ -122,7 +116,7 @@ describe("array", () => {
},
]);

const value = [true, "a"] as InferValue<typeof type>;
const value = [true, "a"] as s.infer<typeof type>;
const parsedValue = type.parse(value);

type Assertions = [
Expand Down Expand Up @@ -174,26 +168,26 @@ describe("array", () => {
});

it('creates discriminated union with nonprimitive "_type" types', () => {
const objectNamedType1 = objectNamed({
const objectNamedType1 = s.objectNamed({
name: "a",
fields: [
{
name: "foo",
type: boolean(),
type: s.boolean(),
},
],
});
const objectNamedType2 = objectNamed({
const objectNamedType2 = s.objectNamed({
name: "b",
fields: [
{
name: "foo",
type: string(),
type: s.string(),
},
],
});

const type = array({
const type = s.array({
of: [objectNamedType1.ref(), objectNamedType2.ref()],
});

Expand All @@ -219,7 +213,7 @@ describe("array", () => {
_type: "b",
foo: "hello",
},
] as InferValue<typeof type>;
] as s.infer<typeof type>;
const parsedValue = type.parse(value);

type Assertions = [
Expand Down Expand Up @@ -296,15 +290,15 @@ describe("array", () => {
});

it("sets min", () => {
const type = array({ min: 2, of: [boolean()] });
const type = s.array({ min: 2, of: [s.boolean()] });

const rule = mockRule();

type.schema().validation(rule);

expect(rule.min).toHaveBeenCalledWith(2);

const value = [true, false] as InferValue<typeof type>;
const value = [true, false] as s.infer<typeof type>;
const parsedValue = type.parse(value);

type Assertions = [
Expand All @@ -320,15 +314,15 @@ describe("array", () => {
});

it("sets max", () => {
const type = array({ max: 3, of: [boolean()] });
const type = s.array({ max: 3, of: [s.boolean()] });

const rule = mockRule();

type.schema().validation(rule);

expect(rule.max).toHaveBeenCalledWith(3);

const value = [true, false, true] as InferValue<typeof type>;
const value = [true, false, true] as s.infer<typeof type>;
const parsedValue = type.parse(value);

type Assertions = [
Expand All @@ -354,15 +348,15 @@ describe("array", () => {
});

it("sets length", () => {
const type = array({ length: 2, of: [boolean()] });
const type = s.array({ length: 2, of: [s.boolean()] });

const rule = mockRule();

type.schema().validation(rule);

expect(rule.length).toHaveBeenCalledWith(2);

const value = [true, false] as InferValue<typeof type>;
const value = [true, false] as s.infer<typeof type>;
const parsedValue = type.parse(value);

type Assertions = [
Expand All @@ -382,9 +376,9 @@ describe("array", () => {
});

it("allows defining the zod", () => {
const type = array({
const type = s.array({
of: [
boolean({ zod: (zod) => zod.transform((value) => (value ? 1 : 0)) }),
s.boolean({ zod: (zod) => zod.transform((value) => (value ? 1 : 0)) }),
],
zod: (zod) =>
zod.transform((values) =>
Expand All @@ -400,21 +394,21 @@ describe("array", () => {
});

it("types custom validation", () => {
const type = array({
const type = s.array({
of: [
object({
s.object({
fields: [
{
name: "foo",
type: boolean(),
type: s.boolean(),
},
],
}),
object({
s.object({
fields: [
{
name: "bar",
type: boolean(),
type: s.boolean(),
},
],
}),
Expand Down
Loading

0 comments on commit 82d072b

Please sign in to comment.