diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml
index 0564ce6..fe2aad4 100644
--- a/.github/workflows/test.yml
+++ b/.github/workflows/test.yml
@@ -4,9 +4,6 @@ on:
pull_request:
push:
-env:
- DENO_VERSION: v1.x
-
jobs:
test:
if: "!contains(github.event.head_commit.message, '[skip ci]')"
@@ -15,7 +12,7 @@ jobs:
- uses: actions/checkout@v3
- uses: denoland/setup-deno@v1
with:
- deno-version: ${{ env.DENO_VERSION }}
+ deno-version: 1.x
- run: deno fmt --check
- run: deno test --coverage=./coverage
- run: deno coverage ./coverage --lcov --output=coverage/lcov.info
@@ -29,26 +26,12 @@ jobs:
env:
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
- publish:
+ publish-jsr:
needs: test
- if: startsWith(github.ref, 'refs/tags/')
runs-on: ubuntu-latest
+ permissions:
+ contents: read
+ id-token: write
steps:
- - uses: actions/checkout@v3
- - uses: pnpm/action-setup@v2.2.2
- with:
- version: 7
- - uses: denoland/setup-deno@v1
- with:
- deno-version: ${{ env.DENO_VERSION }}
- - uses: actions/setup-node@v3
- with:
- node-version: '18.x'
- registry-url: 'https://registry.npmjs.org'
- - run: echo ${{ github.ref_name }} > VERSION
- - run: cat VERSION
- - run: deno run -A bin/build_npm.ts ${{ github.ref_name }}
- - run: npm publish --access=public
- working-directory: ./npm
- env:
- NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
+ - uses: actions/checkout@v4
+ - run: npx jsr publish
diff --git a/README.md b/README.md
index d42ae06..f0b221d 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,9 @@
# Zod Friendly Forms
-[](https://deno.land/x/zod_ff)
-[](https://www.npmjs.com/package/zod-ff)
+[](https://jsr.io/@jmondi/zod-friendly-forms)
+[](https://jsr.io/@jmondi/zod-friendly-forms)
[](https://github.com/jasonraimondi/zod-friendly-forms)
[](https://codeclimate.com/github/jasonraimondi/zod-friendly-forms/test_coverage)
-[](https://www.npmjs.com/package/zod-ff)
Returns an object containing `errors` and `validData`. The `errors` object
contains user-friendly error messages, making it easy to display validation
@@ -16,13 +15,14 @@ submissions.
## Install (npm)
```bash
-pnpm add zod-ff zod
+pnpm add zod
+pnpm dlx jsr add @jmondi/zod-friendly-forms
```
### Deno
-```ts
-import { parseForm } from "https://deno.land/x/zod_ff";
+```bash
+deno add @jmondi/zod-friendly-forms
```
## Usage
@@ -40,7 +40,7 @@ When you're ready to validate your input data, go ahead and run the `parseForm`
function. If there are any errors, they will be available by input key.
```ts
-import { parseForm } from "zod-ff";
+import { parseForm } from "@jmondi/zod-friendly-forms";
const data = {
email: "invalid-email",
@@ -57,7 +57,7 @@ If errors are undefined, the input was valid. A returned `validData` object will
be typed with your response.
```ts
-import { parseForm } from "zod-ff";
+import { parseForm } from "@jmondi/zod-friendly-forms";
const data = {
email: "bob@example.com",
@@ -117,7 +117,7 @@ This library will work on the server or client, in any framework.
```html
```
-
-[deno-package]: https://deno.land/x/zod_ff
-[npm-package]: https://www.npmjs.com/package/zod-ff
-[zod]: https://github.com/colinhacks/zod
diff --git a/bin/build.sh b/bin/build.sh
deleted file mode 100644
index beb4082..0000000
--- a/bin/build.sh
+++ /dev/null
@@ -1,7 +0,0 @@
-#!/bin/bash
-
-set -ex
-
-deno run -A bin/build_npm.ts $1
-cd npm
-pnpm publish --access=publish
diff --git a/bin/build_npm.ts b/bin/build_npm.ts
deleted file mode 100644
index 2600d91..0000000
--- a/bin/build_npm.ts
+++ /dev/null
@@ -1,58 +0,0 @@
-// ex. scripts/build_npm.ts
-import { build, emptyDir } from "https://deno.land/x/dnt/mod.ts";
-
-await emptyDir("./npm");
-
-await build({
- packageManager: "pnpm",
- entryPoints: ["./mod.ts"],
- outDir: "./npm",
- shims: {
- deno: {
- test: true,
- },
- },
- compilerOptions: {
- lib: ["es2021", "dom", "dom.iterable"],
- },
- mappings: {
- "https://deno.land/x/zod@v3.22.4/mod.ts": {
- name: "zod",
- version: "^3.22.4",
- peerDependency: true,
- },
- },
- package: {
- name: "zod-ff",
- version: Deno.args[0]?.replace("v", ""),
- description: "Zod Friendly Forms. Form validation for humans.",
- keywords: ["zod", "zod-plugin", "zod-form-parser"],
- author: "Jason Raimondi ",
- license: "MIT",
- engines: {
- node: ">=18.0.0",
- },
- repository: {
- type: "git",
- url: "git+https://github.com/allmyfutures/zod-friendly-forms.git",
- },
- bugs: {
- url: "https://github.com/allmyfutures/zod-friendly-forms/issues",
- },
- devDependencies: {
- zod: "^3.22.4",
- },
- },
-});
-
-// ensure the test data is ignored in the `.npmignore` file
-// so it doesn't get published with your npm package
-await Deno.writeTextFile(
- "npm/.npmignore",
- "esm/testdata/\nscript/testdata/\n",
- { append: true },
-);
-
-// post build steps
-Deno.copyFileSync("LICENSE", "npm/LICENSE");
-Deno.copyFileSync("README.md", "npm/README.md");
diff --git a/deno.jsonc b/deno.jsonc
index 129dcb3..156b26f 100644
--- a/deno.jsonc
+++ b/deno.jsonc
@@ -1,6 +1,9 @@
{
+ "name": "@jmondi/zod-friendly-forms",
+ "version": "2.0.0-rc.1",
+ "exports": "./mod.ts",
"fmt": {
- "exclude": ["npm/", "README.md"],
+ "exclude": ["README.md"],
"lineWidth": 100,
"indentWidth": 2,
"singleQuote": false,
diff --git a/deno.lock b/deno.lock
index bd7c675..3063f93 100644
--- a/deno.lock
+++ b/deno.lock
@@ -1,5 +1,16 @@
{
"version": "3",
+ "packages": {
+ "specifiers": {
+ "npm:zod@3.22.4": "npm:zod@3.22.4"
+ },
+ "npm": {
+ "zod@3.22.4": {
+ "integrity": "sha512-iC+8Io04lddc+mVqQ9AZ7OQ2MrUKGN+oIQyq1vemgt46jwCwLfhq7/pwnBnNXXXZb8VTVLKwp9EDkx+ryxIWmg==",
+ "dependencies": {}
+ }
+ }
+ },
"remote": {
"https://deno.land/std@0.111.0/_util/assert.ts": "2f868145a042a11d5ad0a3c748dcf580add8a0dbc0e876eaa0026303a5488f58",
"https://deno.land/std@0.111.0/_util/os.ts": "dfb186cc4e968c770ab6cc3288bd65f4871be03b93beecae57d657232ecffcac",
diff --git a/deps.ts b/deps.ts
index a1746bc..6226d02 100644
--- a/deps.ts
+++ b/deps.ts
@@ -1 +1 @@
-export { z } from "https://deno.land/x/zod@v3.22.4/mod.ts";
+export { z } from "npm:zod@3.22.4";
diff --git a/index.ts b/index.ts
index 49fe55d..7af23d5 100644
--- a/index.ts
+++ b/index.ts
@@ -1,10 +1,21 @@
import { z } from "./deps.ts";
+/**
+ * Parameters for parsing a form.
+ * @property schema - The Zod schema to validate the form data.
+ * @property data - The form data to parse.
+ */
export type ParseFormParams = {
schema: z.ZodType;
data: Record | FormData | URLSearchParams;
};
+/**
+ * The result of parsing a form. Either the form data is valid or there are errors.
+ * @property errors - A key-value dictionary of errors.
+ * @property zodError - The Zod error object.
+ * @property validData - The valid form data.
+ */
export type ParseFormResult = {
errors?: never;
zodError?: never;
@@ -15,6 +26,20 @@ export type ParseFormResult = {
validData?: never;
};
+/**
+ * Parse form data using a Zod schema and return a friendly key-value dict of errors
+ * @param params - The parameters for the form parsing.
+ * @param options - Additional options for the form parsing.
+ * @returns The result of the form parsing.
+ * @example """
+ * const schema = z.object({
+ * email: z.string().email(),
+ * password: z.string().min(8),
+ * });
+ * const data = { email: "jason", password: "12345678" };
+ * const { validData, errors } = parseForm({ schema, data });
+ * """
+ */
export function parseForm(
params: ParseFormParams,
options?: { stripEmptyStrings?: boolean },