Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 150 additions & 0 deletions Flaw.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import z from "zod"
import * as gracely from "./index"

type Date = z.infer<typeof Date.validator>
namespace Date {
export const validator = z.string().regex(/(^\d{4}-(0[13-9]|1[012])-([012]\d|30|31)$)|^\d{4}-02-([012]\d)$/)
export function is(value: any): value is Date {
return validator.safeParse(value).success
}
export const flaw = gracely.Flaw.generate("Date string", Date.validator)
}

type Example = z.infer<typeof Example.validator>

namespace Example {
export function is(value: any): value is Example {
return validator.safeParse(value).success
}
export const validator = z.object({
anInteger: z.number().int(),
anOptional: z.number().optional(),
date: Date.validator,
email: z.string().email(),
starter: z.string().startsWith("left_"),
test: z
.object({
deeper: z.boolean(),
})
.optional(),
})
export const flaw = gracely.Flaw.generate("model.Example", validator)
}

describe("Flaw tests", () => {
it("Flaw self tests", () => {
expect(gracely.Flaw.is({})).toBeFalsy()
expect(gracely.Flaw.flaw({})).toEqual({
flaws: [
{
condition: "Required",
property: "type",
type: "string",
},
],
type: "gracely.Flaw",
})
const veryFlawedFlaw = {
type: "example",
flaws: [{ type: "example", flaws: [{ epyt: "reversed", flaws: true }] }],
}
expect(gracely.Flaw.is(veryFlawedFlaw)).toBeFalsy()
expect(gracely.Flaw.flaw(veryFlawedFlaw)).toEqual({
flaws: [
{
condition: "Required",
property: "flaws.0.flaws.0.type",
type: "string",
},
{
condition: "Expected array, received boolean",
property: "flaws.0.flaws.0.flaws",
type: "array",
},
],
type: "gracely.Flaw",
})
})
it("Flaw.generate tests", () => {
const example: Example = {
anInteger: 1,
date: "2022-01-31",
email: "is@valid.mail",
starter: "left_right",
}
const modified: Record<string, any> = { ...example }
expect(Example.is(example)).toBeTruthy()
expect(Example.flaw(example)).toEqual({
type: "model.Example",
flaws: [],
})
modified.email = "isn't valid email"
modified.test = "test"
expect(Example.is(modified)).toBeFalsy()
expect(Example.flaw(modified)).toEqual({
type: "model.Example",
flaws: [
{
condition: "Invalid email",
property: "email",
type: "email",
},
{
condition: "Expected object, received string",
property: "test",
type: "object",
},
],
})
modified.email = "is@valid.mail"
modified.test = { deeper: true }
modified.starter = "right_left"
expect(Example.is(modified)).toBeFalsy()
expect(Example.flaw(modified)).toEqual({
type: "model.Example",
flaws: [
{
condition: 'Invalid input: must start with "left_"',
property: "starter",
type: '{"startsWith":"left_"}',
},
],
})
modified.test.deeper = "true"
expect(Example.is(modified)).toBeFalsy()
expect(Example.flaw(modified)).toEqual({
type: "model.Example",
flaws: [
{
condition: 'Invalid input: must start with "left_"',
property: "starter",
type: '{"startsWith":"left_"}',
},
{
condition: "Expected boolean, received string",
property: "test.deeper",
type: "boolean",
},
],
})
modified.starter = "left_something"
modified.test.deeper = 1
modified.date = "2022-02-30"
expect(Example.is(modified)).toBeFalsy()
expect(Example.flaw(modified)).toEqual({
type: "model.Example",
flaws: [
{
condition: "Invalid",
property: "date",
type: "regex",
},
{
condition: "Expected boolean, received number",
property: "test.deeper",
type: "boolean",
},
],
})
})
})
42 changes: 34 additions & 8 deletions Flaw.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import z from "zod"

export interface Flaw {
property?: string
type: string
flaws?: Flaw[]
condition?: string
}

export namespace Flaw {
export const validator: z.ZodType<Flaw> = z.lazy(() =>
z.object({
property: z.string().optional(),
type: z.string(),
flaws: z.array(validator).optional(), // recursive type makes type inference impossible
condition: z.string().optional(),
})
)
export function is(value: any | Flaw): value is Flaw {
return (
typeof value == "object" &&
(value.property == undefined || typeof value.property == "string") &&
typeof value.type == "string" &&
(value.flaws == undefined || (Array.isArray(value.flaws) && value.flaws.every(Flaw.is))) &&
(value.condition == undefined || typeof value.condition == "string")
)
return validator.safeParse(value).success
}
export const flaw = generate("gracely.Flaw", validator)
export function generate(type: string, validator: z.ZodType): (value: any) => Flaw {
return (value: any) => {
const parsed = validator.safeParse(value)
return {
type,
flaws: !("error" in parsed)
? []
: parsed.error.errors.map(error => ({
property: error.path.join("."),
type:
"expected" in error && typeof error.expected == "string"
? error.expected
: !("validation" in error)
? "unknown"
: typeof error.validation == "string"
? error.validation
: JSON.stringify(error.validation),
condition: error.message,
})),
}
}
}
}
16 changes: 16 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,8 @@
"rimraf": "^3.0.2",
"ts-jest": "^28.0.7",
"typescript": "^4.7.4"
},
"dependencies": {
"zod": "^3.18.0"
}
}