/
README.test.ts
22 lines (18 loc) · 962 Bytes
/
README.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import { ShapeToType, ShapeValidationError, s, validateDataShape } from "../src"
test("README example", () => {
const resourceShape = s.object({
id: s.string({pattern: /^[a-zA-Z0-9\-_]{10}$/}),
state: s.union([s.literal("pending"), s.literal("active"), s.literal("removed")]),
createdAt: s.integer()
})
type Resource = ShapeToType<typeof resourceShape>
const goodData = JSON.parse("{\"id\":\"ui_1zoEJ18\",\"state\":\"active\",\"createdAt\":1700354795466}")
const resource: Resource = validateDataShape({ data: goodData, shape: resourceShape })
expect(resource.id).toStrictEqual("ui_1zoEJ18")
const badData = JSON.parse("{\"id\":\"\",\"state\":\"active\",\"createdAt\":1700354795466}")
expect(() => validateDataShape({ data: badData, shape: resourceShape })).toThrow(new ShapeValidationError({
path: ["id"],
data: "",
shape: s.string({pattern: /^[a-zA-Z0-9\-_]{10}$/})
}))
})