Skip to content

Commit

Permalink
Remove yup
Browse files Browse the repository at this point in the history
  • Loading branch information
EddieAbbondanzio committed Sep 18, 2022
1 parent 242fddd commit b5f8ec1
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 67 deletions.
25 changes: 0 additions & 25 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@
"sass-loader": "^12.3.0",
"styled-components": "^5.3.3",
"unist-util-visit": "^2.0.3",
"yup": "^1.0.0-beta.4",
"zod": "^3.17.10"
}
}
4 changes: 2 additions & 2 deletions src/main/notes.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
createNote,
getNoteById,
getNoteSchema,
Note,
NOTE_SCHEMA,
} from "../shared/domain/note";
import { UUID_REGEX } from "../shared/domain";
import { Config } from "../shared/domain/config";
Expand Down Expand Up @@ -188,7 +188,7 @@ export async function loadNotes(dataDirectory: string): Promise<Note[]> {
dateUpdated: dateUpdated != null ? parseJSON(dateUpdated) : undefined,
...remainder,
});
await getNoteSchema().validate(note);
await NOTE_SCHEMA.parseAsync(note);

// We don't add children until every note has been loaded because there's a
// chance children will be loaded before their parent.
Expand Down
10 changes: 4 additions & 6 deletions src/renderer/components/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ import { clamp, Dictionary, head, isEmpty, keyBy, take } from "lodash";
import {
Note,
getNoteById,
getNoteSchema,
flatten,
sortNotes,
DEFAULT_NOTE_SORTING_ALGORITHM,
getParents,
NOTE_SCHEMA,
} from "../../shared/domain/note";
import { createPromisedInput, PromisedInput } from "../../shared/promisedInput";
import { promptError, promptConfirmAction } from "../utils/prompt";
import { Scrollable } from "./shared/Scrollable";
import * as yup from "yup";
import { SIDEBAR_MENU_HEIGHT, SidebarMenu, SidebarInput } from "./SidebarMenu";
import {
faChevronDown,
Expand All @@ -28,6 +27,7 @@ import { search } from "fast-fuzzy";
import { filterOutStaleNoteIds } from "../../shared/ui/app";
import { SidebarNewNoteButton } from "./SidebarNewNoteButton";
import { Section } from "../../shared/ui/app";
import { z } from "zod";

const EXPANDED_ICON = faChevronDown;
const COLLAPSED_ICON = faChevronRight;
Expand Down Expand Up @@ -398,10 +398,9 @@ export const createNote: Listener<"sidebar.createNote"> = async (
) => {
const { sidebar } = ctx.getState();

const schema: yup.StringSchema = yup.reach(getNoteSchema(), "name");
const input = createPromisedInput(
{
schema,
schema: NOTE_SCHEMA.shape.name,
parentId: parentId ?? undefined,
},
setExplorerInput(ctx)
Expand Down Expand Up @@ -478,13 +477,12 @@ export const renameNote: Listener<"sidebar.renameNote"> = async (
}

const { notes } = ctx.getState();
const schema: yup.StringSchema = yup.reach(getNoteSchema(), "name");
const { name: value } = getNoteById(notes, id!);
const input = createPromisedInput(
{
id,
value,
schema,
schema: NOTE_SCHEMA.shape.name,
},
setExplorerInput(ctx)
);
Expand Down
7 changes: 2 additions & 5 deletions src/shared/domain/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { customAlphabet } from "nanoid";
import * as yup from "yup";
import { z } from "zod";

const ID_ALPHABET =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
Expand All @@ -8,10 +8,7 @@ const ID_LENGTH = 10;
export const uuid = customAlphabet(ID_ALPHABET, ID_LENGTH);
export const UUID_REGEX = /[a-zA-Z0-9]{10}$/;

export const uuidSchema = yup
.string()
.required()
.test((val) => UUID_REGEX.test(val ?? ""));
export const UUID_SCHEMA = z.string().refine((val) => UUID_REGEX.test(val));

export interface Resource {
id: string;
Expand Down
38 changes: 15 additions & 23 deletions src/shared/domain/note.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as yup from "yup";
import { uuidSchema, Resource, uuid } from ".";
import { UUID_SCHEMA, Resource, uuid } from ".";
import { isBlank } from "../utils";
import { isEmpty, orderBy } from "lodash";
import { z } from "zod";

export interface Note extends Resource {
name: string;
Expand Down Expand Up @@ -104,27 +104,19 @@ export function createNote(props: Partial<Note> & { name: string }): Note {
return note;
}

// TODO: Replace with zod
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getNoteSchema(): any {
return yup
.object()
.shape({
id: uuidSchema,
// Name is not unique because it's difficult to enforce uniqueness when
// notes can change parents. There's no real harm in having duplicates.
name: yup
.string()
.required("Name is required.")
.min(1, "Note name must be at least 1 character.")
.max(64, "Note name cannot be more than 64 characters."),
flags: yup.number(),
dateCreated: yup.date().required(),
dateUpdated: yup.date().optional(),
sort: yup.mixed().optional().oneOf(Object.values(NoteSort)),
})
.defined();
}
export const NOTE_SCHEMA = z.object({
id: UUID_SCHEMA,
// Name is not unique because it's difficult to enforce uniqueness when
// notes can change parents. There's no real harm in having duplicates.
name: z
.string()
.min(1, "Name must be at least 1 char long")
.max(64, "Name must be 64 chars or less."),
flags: z.number().optional(),
dateCreated: z.date(),
dateUpdated: z.date().optional(),
sort: z.nativeEnum(NoteSort).optional(),
});

/**
* Recursively search for a note based on it's id.
Expand Down
17 changes: 12 additions & 5 deletions src/shared/promisedInput.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { isEmpty } from "lodash";
import { Nullable } from "tsdef";
import * as yup from "yup";
import { z, ZodError } from "zod";

// TODO: Can we find a better spot for this? The file feels out of place.

Expand All @@ -21,7 +22,7 @@ type ValidateOutcome = { valid: true } | { valid: false; errors: string[] };
export type PromisedInputParams = {
value?: string;
id?: string;
schema: yup.StringSchema;
schema: z.ZodString | z.ZodNumber;
parentId?: string;
};
export type PromisedOutcome = "confirm" | "cancel";
Expand All @@ -40,6 +41,10 @@ export function createPromisedInput(
new Promise(
(res) =>
(confirm = () => {
if (isEmpty(value)) {
return;
}

outcome = "confirm";
res([value, "confirm"]);
})
Expand All @@ -64,11 +69,13 @@ export function createPromisedInput(
const promise = Promise.race([confirmPromise, cancelPromise]);
const validate = (value: string): ValidateOutcome => {
try {
params.schema.validateSync(value);
params.schema.parse(value);
return { valid: true };
} catch (e) {
const { errors } = e as yup.ValidationError;
return { valid: false, errors };
return {
valid: false,
errors: (e as ZodError).issues.map((i) => i.message),
};
}
};

Expand Down
6 changes: 6 additions & 0 deletions test/shared/domain/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { UUID_SCHEMA } from "../../../src/shared/domain";

test("uuidSchema", async () => {
const uuid = "d3ZU8GmTG3";
expect(await UUID_SCHEMA.parseAsync(uuid)).toBe("d3ZU8GmTG3");
});

0 comments on commit b5f8ec1

Please sign in to comment.