Skip to content

Commit

Permalink
feat: built-in support for multiple validation libraries (#202)
Browse files Browse the repository at this point in the history
Make use of built-in modular support for multiple validation libraries, instead of TypeSchema.
  • Loading branch information
TheEdoRan authored Jul 20, 2024
1 parent e00829a commit baff76b
Show file tree
Hide file tree
Showing 54 changed files with 612 additions and 2,817 deletions.
4 changes: 2 additions & 2 deletions apps/playground/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
"zod-form-data": "^2.0.2"
},
"devDependencies": {
"@types/node": "^20.12.10",
"@types/node": "^20.14.11",
"@types/react": "^18.3.1",
"@types/react-dom": "18.3.0",
"autoprefixer": "10.4.19",
"eslint": "^8.57.0",
"eslint-config-next": "15.0.0-canary.25",
"postcss": "8.4.38",
"tailwindcss": "3.4.3",
"typescript": "^5.4.5"
"typescript": "^5.5.3"
}
}
2 changes: 2 additions & 0 deletions apps/playground/src/lib/safe-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import {
DEFAULT_SERVER_ERROR_MESSAGE,
createSafeActionClient,
} from "next-safe-action";
import { zodAdapter } from "next-safe-action/adapters/zod";
import { z } from "zod";

export class ActionError extends Error {}

export const action = createSafeActionClient({
validationAdapter: zodAdapter(),
// You can provide a custom logging function, otherwise the lib will use `console.error`
// as the default logging system. If you want to disable server errors logging,
// just pass an empty Promise.
Expand Down
33 changes: 18 additions & 15 deletions packages/next-safe-action/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@
],
"exports": {
".": "./dist/index.mjs",
"./typeschema": "./dist/typeschema.mjs",
"./hooks": "./dist/hooks.mjs",
"./stateful-hooks": "./dist/stateful-hooks.mjs"
"./stateful-hooks": "./dist/stateful-hooks.mjs",
"./adapters/*": "./dist/adapters/*.mjs"
},
"typesVersions": {
"*": {
".": [
"./dist/index.d.mts"
],
"typeschema": [
"./dist/typeschema.d.mts"
],
"hooks": [
"./dist/hooks.d.mts"
],
"stateful-hooks": [
"./dist/stateful-hooks.d.mts"
],
"adapters/*": [
"./dist/adapters/*.d.mts"
]
}
},
Expand All @@ -43,7 +43,7 @@
],
"scripts": {
"lint": "tsc && prettier --write . && eslint .",
"test": "node --import tsx --test ./src/__tests__/*.test.ts ./src/__tests__/typeschema/*.test.ts",
"test": "node --import tsx --test ./src/__tests__/*.test.ts",
"build": "tsup",
"deploy": "semantic-release"
},
Expand All @@ -67,10 +67,9 @@
},
"devDependencies": {
"@eslint/js": "^9.2.0",
"@types/node": "^20.12.10",
"@types/node": "^20.14.11",
"@types/react": "^18.3.1",
"@types/react-dom": "18.3.0",
"@typeschema/core": "^0.13.2",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-define-config": "^2.1.0",
Expand All @@ -82,26 +81,30 @@
"semantic-release": "^23.0.8",
"tsup": "^8.0.2",
"tsx": "^4.11.2",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
"typescript": "^5.5.3",
"typescript-eslint": "^7.8.0",
"valibot": "^0.36.0",
"yup": "^1.4.0",
"zod": "^3.23.6"
},
"peerDependencies": {
"next": ">= 14.0.0",
"react": ">= 18.2.0",
"react-dom": ">= 18.2.0",
"zod": ">= 3.0.0"
"valibot": ">= 0.36.0",
"zod": ">= 3.0.0",
"yup": ">= 1.0.0"
},
"peerDependenciesMeta": {
"zod": {
"optional": true
},
"valibot": {
"optional": true
}
},
"repository": {
"type": "git",
"url": "https://github.com/TheEdoRan/next-safe-action.git"
},
"dependencies": {
"@typeschema/main": "^0.13.10",
"@typeschema/zod": "^0.13.3"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import assert from "node:assert";
import { test } from "node:test";
import { z } from "zod";
import { DEFAULT_SERVER_ERROR_MESSAGE, createSafeActionClient, returnValidationErrors } from "..";
import { zodAdapter } from "../adapters/zod";

const ac = createSafeActionClient({
validationAdapter: zodAdapter(),
defineMetadataSchema() {
return z.object({
actionName: z.string(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import assert from "node:assert";
import { test } from "node:test";
import { z } from "zod";
import { createSafeActionClient, flattenBindArgsValidationErrors, formatBindArgsValidationErrors } from "..";
import { zodAdapter } from "../adapters/zod";

// Default client tests.

const dac = createSafeActionClient();
const dac = createSafeActionClient({
validationAdapter: zodAdapter(),
});

test("action with invalid bind args input gives back an object with correct `bindArgsValidationErrors` (default formatted shape)", async () => {
const bindArgsSchemas: [age: z.ZodNumber, userId: z.ZodString, product: z.ZodObject<{ id: z.ZodString }>] = [
Expand Down Expand Up @@ -87,6 +90,7 @@ test("action with invalid bind args input gives back an object with correct `bin
// Formatted shape tests (same as default).

const foac = createSafeActionClient({
validationAdapter: zodAdapter(),
defaultValidationErrorsShape: "formatted",
});

Expand Down Expand Up @@ -168,6 +172,7 @@ test("action with invalid bind args input gives back an object with correct `bin
// Flattened shape tests.

const flac = createSafeActionClient({
validationAdapter: zodAdapter(),
defaultValidationErrorsShape: "flattened",
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import {
formatBindArgsValidationErrors,
formatValidationErrors,
} from "..";
import { zodAdapter } from "../adapters/zod";

// Default client tests.

const dac = createSafeActionClient();
const dac = createSafeActionClient({
validationAdapter: zodAdapter(),
});

test("action with invalid bind args input and valid main input gives back an object with correct `bindArgsValidationErrors` (default formatted shape)", async () => {
const schema = z.object({
Expand Down Expand Up @@ -110,6 +113,7 @@ test("action with invalid bind args input and invalid main input gives back an o
// Formatted shape tests (same as default).

const foac = createSafeActionClient({
validationAdapter: zodAdapter(),
defaultValidationErrorsShape: "formatted",
});

Expand Down Expand Up @@ -209,6 +213,7 @@ test("action with invalid bind args input and valid main input gives back an obj
// Flattened shape tests.

const flac = createSafeActionClient({
validationAdapter: zodAdapter(),
defaultValidationErrorsShape: "flattened",
});

Expand Down
5 changes: 4 additions & 1 deletion packages/next-safe-action/src/__tests__/happy-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import assert from "node:assert";
import { test } from "node:test";
import { z } from "zod";
import { createSafeActionClient } from "..";
import { zodAdapter } from "../adapters/zod";

const ac = createSafeActionClient();
const ac = createSafeActionClient({
validationAdapter: zodAdapter(),
});

test("action with no input schema returns empty object", async () => {
const action = ac.action(async () => {
Expand Down
2 changes: 2 additions & 0 deletions packages/next-safe-action/src/__tests__/metadata.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import assert from "node:assert";
import { test } from "node:test";
import { z } from "zod";
import { DEFAULT_SERVER_ERROR_MESSAGE, createSafeActionClient } from "..";
import { zodAdapter } from "../adapters/zod";

const ac = createSafeActionClient({
validationAdapter: zodAdapter(),
handleServerErrorLog() {}, // disable server errors logging for these tests
defineMetadataSchema() {
return z.object({
Expand Down
3 changes: 3 additions & 0 deletions packages/next-safe-action/src/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import {
formatValidationErrors,
returnValidationErrors,
} from "..";
import { zodAdapter } from "../adapters/zod";

const ac = createSafeActionClient({
validationAdapter: zodAdapter(),
handleServerErrorLog() {}, // disable server errors logging for these tests
handleReturnedServerError(e) {
return {
Expand Down Expand Up @@ -292,6 +294,7 @@ test("server validation errors in execution result from middleware are correct",
// Flattened validation errors shape.

const flac = createSafeActionClient({
validationAdapter: zodAdapter(),
handleServerErrorLog() {}, // disable server errors logging for these tests
defaultValidationErrorsShape: "flattened",
});
Expand Down
4 changes: 4 additions & 0 deletions packages/next-safe-action/src/__tests__/server-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import assert from "node:assert";
import { test } from "node:test";
import { DEFAULT_SERVER_ERROR_MESSAGE, createSafeActionClient } from "..";
import { zodAdapter } from "../adapters/zod";

class ActionError extends Error {
constructor(message: string) {
Expand All @@ -11,6 +12,7 @@ class ActionError extends Error {
}

const ac1 = createSafeActionClient({
validationAdapter: zodAdapter(),
handleServerErrorLog: () => {}, // disable server errors logging for these tests
handleReturnedServerError(e) {
if (e instanceof ActionError) {
Expand Down Expand Up @@ -93,6 +95,7 @@ test("known error occurred in middleware function is unmasked", async () => {

// Server error is an object with a 'message' property.
const ac2 = createSafeActionClient({
validationAdapter: zodAdapter(),
handleServerErrorLog: () => {}, // disable server errors logging for these tests
handleReturnedServerError(e) {
return {
Expand Down Expand Up @@ -138,6 +141,7 @@ test("error occurred in middleware function has the correct shape defined by `ha

// Rethrow all server errors.
const ac3 = createSafeActionClient({
validationAdapter: zodAdapter(),
handleServerErrorLog: () => {}, // disable server errors logging for these tests
handleReturnedServerError(e) {
throw e;
Expand Down
Loading

0 comments on commit baff76b

Please sign in to comment.