-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also: - docs: adds demo
- Loading branch information
Showing
13 changed files
with
123 additions
and
16 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 |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = { extends: ["@commitlint/config-conventional"] }; | ||
export default { extends: ["@commitlint/config-conventional"] }; |
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,27 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>Zodex Demo</title> | ||
<style> | ||
textarea { width: 300px; height: 400px; } | ||
</style> | ||
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon" /> | ||
<script type="importmap"> | ||
{ | ||
"imports": { | ||
"zod": "../node_modules/zod/lib/index.mjs", | ||
"react": "./react.js" | ||
} | ||
} | ||
</script> | ||
<script type="module" src="index.js"></script> | ||
</head> | ||
<body> | ||
<textarea id="js" placeholder="Put arbitrary JavaScript object here (e.g., `{a: 5}`)">{a: 5}</textarea> | ||
<textarea id="zod" placeholder="Put Zod here (e.g., `z.object({a: z.number()})`">z.object({a: z.number()})</textarea> | ||
<button id="zerialize">Zerialize</button> | ||
<textarea id="zodexJSON" placeholder="Put Zodex JSON here (or zerialize a Zod expression)"></textarea> | ||
<button id="dezerialize">Dezerialize and validate</button> | ||
</body> | ||
</html> |
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,66 @@ | ||
import { z } from "zod"; | ||
import { zerialize, dezerialize } from "../dist/esm/index.js"; | ||
|
||
const $ = (sel) => { | ||
return document.querySelector(sel); | ||
}; | ||
|
||
$("#dezerialize").addEventListener("click", () => { | ||
let js; | ||
try { | ||
js = eval(`(${$("#js").value})`); | ||
} catch (err) { | ||
alert("Error evaluating JavaScript"); | ||
return; | ||
} | ||
|
||
let json; | ||
try { | ||
json = JSON.parse($("#zodexJSON").value); | ||
} catch (err) { | ||
alert("Error evaluating JSON"); | ||
return; | ||
} | ||
|
||
let dez; | ||
try { | ||
dez = dezerialize(json); | ||
} catch (err) { | ||
alert("Error dezerializing JSON"); | ||
return; | ||
} | ||
|
||
let parseObj; | ||
try { | ||
parseObj = dez.safeParse(js); | ||
} catch (err) { | ||
alert("Error parsing JavaScript"); | ||
return; | ||
} | ||
|
||
alert(JSON.stringify(parseObj, null, 2)); | ||
}); | ||
|
||
$("#zerialize").addEventListener("click", () => { | ||
let zod; | ||
try { | ||
zod = eval(`(${$("#zod").value})`); | ||
} catch (err) { | ||
alert("Error evaluating Zod"); | ||
return; | ||
} | ||
let zer; | ||
try { | ||
zer = zerialize(zod); | ||
} catch (err) { | ||
alert("Error zerializing Zod"); | ||
return; | ||
} | ||
|
||
try { | ||
$("#zodexJSON").value = JSON.stringify(zer, null, 2); | ||
} catch (err) { | ||
alert("Error stringifying Zod JSON object"); | ||
return; | ||
} | ||
}); |
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 @@ | ||
export default {}; |
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 |
---|---|---|
|
@@ -2,8 +2,16 @@ | |
"name": "zodex", | ||
"version": "0.0.0-dev", | ||
"description": "Type-safe (de)serialization for Zod", | ||
"type": "module", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"exports": { | ||
".": { | ||
"import": "./dist/esm/index.js", | ||
"require": "./dist/index.js" | ||
}, | ||
"./dist/schema.zodex.json": "./dist/schema.zodex.json" | ||
}, | ||
"keywords": [ | ||
"zod", | ||
"serialization", | ||
|
@@ -13,11 +21,12 @@ | |
"author": "Gregor Weber<[email protected]>", | ||
"license": "MIT", | ||
"scripts": { | ||
"start": "vite serve demo", | ||
"prepare": "husky install", | ||
"check-style": "prettier --check src", | ||
"lint": "eslint src/**", | ||
"test": "vitest --coverage --silent=false --reporter=basic", | ||
"build": "rm -rf dist && pnpm tsc && cp src/schema.zodex.json dist/schema.zodex.json", | ||
"build": "rm -rf dist && pnpm tsc --module esnext --moduleResolution bundler --outDir dist/esm && pnpm tsc && echo '{\"type\": \"commonjs\"}' > dist/package.json && echo '{\"type\": \"module\"}' > dist/esm/package.json && cp src/schema.zodex.json dist/schema.zodex.json", | ||
"prepublish": "pnpm run build" | ||
}, | ||
"peerDependencies": { | ||
|
@@ -44,6 +53,7 @@ | |
"lint-staged": "15.2.7", | ||
"prettier": "2.8.8", | ||
"typescript": "5.3.x", | ||
"vite": "^5.3.1", | ||
"vitest": "1.6.0" | ||
}, | ||
"packageManager": "[email protected]", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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