-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): ensure result is valid json
Validate the resulting objects and ensure that they can be serialized to json
- Loading branch information
Showing
11 changed files
with
238 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@content-collections/core": minor | ||
--- | ||
|
||
Validate the resulting objects and ensure that they can be serialized to json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ describe("simple", () => { | |
expect(post).toEqual({ | ||
title: "Post One", | ||
description: "This is the first post", | ||
date: "2019-01-01T00:00:00.000Z", | ||
date: "2019-01-01", | ||
author: { | ||
displayName: "Tricia Marie McMillan", | ||
email: "[email protected]", | ||
|
@@ -40,7 +40,7 @@ describe("simple", () => { | |
expect(post).toEqual({ | ||
title: "Post Two", | ||
description: "This is the second post", | ||
date: "2020-01-01T00:00:00.000Z", | ||
date: "2020-01-01", | ||
author: { | ||
displayName: "Tricia Marie McMillan", | ||
email: "[email protected]", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import { defineCollection } from "@content-collections/core"; | ||
|
||
export default defineCollection({ | ||
const collection = defineCollection({ | ||
name: "posts", | ||
typeName: "Post", | ||
schema: (z) => ({ | ||
title: z.string().min(5), | ||
description: z.string().min(10), | ||
date: z | ||
.union([z.string().regex(/^\d{4}-\d{2}-\d{2}$/), z.date()]) | ||
.transform((val) => new Date(val)), | ||
date: z.string().regex(/^\d{4}-\d{2}-\d{2}$/), | ||
}), | ||
transform: (doc) => { | ||
return { | ||
...doc, | ||
}; | ||
}, | ||
directory: "posts", | ||
include: "**/*.md(x)?", | ||
}); | ||
|
||
export default collection; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, it, expect } from "vitest"; | ||
import { jsonObjectScheme } from "./json"; | ||
|
||
describe("json", () => { | ||
it("should pass valid json", () => { | ||
const json = { | ||
a: 1, | ||
b: "string", | ||
c: true, | ||
d: null, | ||
e: { | ||
f: "nested", | ||
}, | ||
g: [1, 2, 3], | ||
}; | ||
|
||
const result = jsonObjectScheme.safeParse(json); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should allow undefined values", () => { | ||
const json = { | ||
a: undefined, | ||
}; | ||
|
||
const result = jsonObjectScheme.safeParse(json); | ||
expect(result.success).toBe(true); | ||
}); | ||
|
||
it("should fail if object contains a date object", () => { | ||
const json = { | ||
a: new Date(), | ||
}; | ||
|
||
const result = jsonObjectScheme.safeParse(json); | ||
expect(result.success).toBe(false); | ||
}); | ||
|
||
it("should fail if object contains a function", () => { | ||
const json = { | ||
a: () => {}, | ||
}; | ||
|
||
const result = jsonObjectScheme.safeParse(json); | ||
expect(result.success).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import z from "zod"; | ||
|
||
const literalSchema = z.union([ | ||
z.string(), | ||
z.number(), | ||
z.boolean(), | ||
z.null(), | ||
z.undefined(), | ||
]); | ||
|
||
type Literal = z.infer<typeof literalSchema>; | ||
|
||
type Json = Literal | { [key: string]: Json } | Json[]; | ||
|
||
|
||
const jsonSchema: z.ZodType<Json> = z.lazy(() => | ||
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)]) | ||
); | ||
|
||
export const jsonObjectScheme = z.record(jsonSchema); | ||
|
||
export type JSONObject = z.infer<typeof jsonObjectScheme>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.