@nathan3boss/expressu 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 nathan3boss
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,106 @@
1
+ # expressu
2
+
3
+ ![npm version](https://img.shields.io/npm/v/expressu?style=flat&logo=npm)
4
+ ![CI](https://img.shields.io/github/actions/workflow/status/nathan2slime/expressu/ci.yml?branch=main&style=flat&logo=github)
5
+
6
+ Composable Express middleware builders for request validators.
7
+
8
+ ## What it does
9
+
10
+ - Validates `body`, `params`, and `query`.
11
+ - Supports a global validation message override.
12
+ - Keeps the core adapter-agnostic.
13
+ - Ships adapters for Zod, Yup, and class-transformer.
14
+
15
+ ## Global config
16
+
17
+ Set a default validation message once at app startup.
18
+
19
+ ```ts
20
+ import { configure } from "expressu";
21
+
22
+ configure({ validationMessage: "Dados inválidos" });
23
+ ```
24
+
25
+ ## Core usage
26
+
27
+ ```ts
28
+ import express from "express";
29
+ import { createBodyValidator, createValidationAdapter } from "expressu";
30
+
31
+ const app = express();
32
+
33
+ const adapter = createValidationAdapter((input: { userId: string }) => ({
34
+ userId: Number(input.userId),
35
+ }));
36
+
37
+ app.use(express.json());
38
+
39
+ app.post("/users", createBodyValidator(adapter), (req, res) => {
40
+ res.json(req.body);
41
+ });
42
+ ```
43
+
44
+ ### zod adapter
45
+
46
+ ```ts
47
+ import express from "express";
48
+ import { z } from "zod";
49
+ import { createBodyValidator } from "expressu";
50
+ import { createZodAdapter } from "expressu/zod";
51
+
52
+ const app = express();
53
+ const schema = z.object({ userId: z.coerce.number().int() });
54
+
55
+ app.use(express.json());
56
+ app.post("/users", createBodyValidator(createZodAdapter(schema)), (req, res) => {
57
+ res.json(req.body);
58
+ });
59
+ ```
60
+
61
+ ### yup adapter
62
+
63
+ ```ts
64
+ import * as yup from "yup";
65
+ import { createBodyValidator } from "expressu";
66
+ import { createYupAdapter } from "expressu/yup";
67
+
68
+ const schema = yup.object({ userId: yup.number().required().integer() });
69
+
70
+ app.post("/users", createBodyValidator(createYupAdapter(schema)), (req, res) => {
71
+ res.json(req.body);
72
+ });
73
+ ```
74
+
75
+ ### class-transformer adapter
76
+
77
+ ```ts
78
+ import { createBodyValidator } from "expressu";
79
+ import { createClassTransformerAdapter } from "expressu/class-transformer";
80
+
81
+ class UserDto {
82
+ id!: number;
83
+ name!: string;
84
+ }
85
+
86
+ app.post(
87
+ "/users",
88
+ createBodyValidator(
89
+ createClassTransformerAdapter(UserDto, {
90
+ validate: (value) => value.id > 0 && value.name.length > 0,
91
+ }),
92
+ ),
93
+ (req, res) => {
94
+ res.json(req.body);
95
+ },
96
+ );
97
+ ```
98
+
99
+
100
+ ## Scripts
101
+
102
+ - `pnpm build`
103
+ - `pnpm typecheck`
104
+ - `pnpm lint`
105
+ - `pnpm test`
106
+ - `pnpm test:coverage`
@@ -0,0 +1,36 @@
1
+ import { type ClassTransformOptions } from "class-transformer";
2
+ import type { ValidationAdapter } from "../types";
3
+ import { createBodyValidator, createParamsValidator, createQueryValidator, createValidatorMiddleware } from "../validator";
4
+ type ClassConstructor<TOutput> = new () => TOutput;
5
+ /**
6
+ * Optional overrides supported by the class-transformer adapter.
7
+ */
8
+ type ClassTransformerAdapterOverrides<TOutput> = Partial<{
9
+ message: string;
10
+ validate: (value: TOutput) => boolean;
11
+ }>;
12
+ /**
13
+ * Adapter options for class-transformer-based validators.
14
+ */
15
+ export type ClassTransformerAdapterOptions<TOutput> = ClassTransformOptions & ClassTransformerAdapterOverrides<TOutput>;
16
+ /**
17
+ * Creates a class-transformer-backed validation adapter.
18
+ */
19
+ export declare const createClassTransformerAdapter: <TOutput>(target: ClassConstructor<TOutput>, options?: ClassTransformerAdapterOptions<TOutput>) => ValidationAdapter<unknown, TOutput>;
20
+ /**
21
+ * Builds middleware for any request target using class-transformer.
22
+ */
23
+ export declare const classTransformerMiddleware: <TOutput>(target: ClassConstructor<TOutput>, options?: ClassTransformerAdapterOptions<TOutput>) => ReturnType<typeof createValidatorMiddleware>;
24
+ /**
25
+ * Builds middleware that validates `req.body` with class-transformer.
26
+ */
27
+ export declare const classTransformerBodyValidator: <TOutput>(target: ClassConstructor<TOutput>, options?: ClassTransformerAdapterOptions<TOutput>) => ReturnType<typeof createBodyValidator>;
28
+ /**
29
+ * Builds middleware that validates `req.params` with class-transformer.
30
+ */
31
+ export declare const classTransformerParamsValidator: <TOutput>(target: ClassConstructor<TOutput>, options?: ClassTransformerAdapterOptions<TOutput>) => ReturnType<typeof createParamsValidator>;
32
+ /**
33
+ * Builds middleware that validates `req.query` with class-transformer.
34
+ */
35
+ export declare const classTransformerQueryValidator: <TOutput>(target: ClassConstructor<TOutput>, options?: ClassTransformerAdapterOptions<TOutput>) => ReturnType<typeof createQueryValidator>;
36
+ export {};
@@ -0,0 +1,36 @@
1
+ import type { ValidationAdapter } from "../types";
2
+ import { createBodyValidator, createParamsValidator, createQueryValidator, createValidatorMiddleware } from "../validator";
3
+ /**
4
+ * Options accepted by the Yup adapter.
5
+ */
6
+ export type YupAdapterOptions = Partial<{
7
+ abortEarly: boolean;
8
+ context: Record<string, unknown>;
9
+ message: string;
10
+ strict: boolean;
11
+ stripUnknown: boolean;
12
+ }>;
13
+ type YupSchemaLike<TOutput> = {
14
+ validateSync(input: unknown, options?: YupAdapterOptions): TOutput;
15
+ };
16
+ /**
17
+ * Creates a Yup-backed validation adapter.
18
+ */
19
+ export declare const createYupAdapter: <TOutput, TSchema extends YupSchemaLike<TOutput>>(schema: TSchema, options?: YupAdapterOptions) => ValidationAdapter<unknown, TOutput>;
20
+ /**
21
+ * Builds middleware for any request target using a Yup schema.
22
+ */
23
+ export declare const yupMiddleware: <TOutput, TSchema extends YupSchemaLike<TOutput>>(schema: TSchema, options?: YupAdapterOptions) => ReturnType<typeof createValidatorMiddleware>;
24
+ /**
25
+ * Builds middleware that validates `req.body` with Yup.
26
+ */
27
+ export declare const yupBodyValidator: <TOutput, TSchema extends YupSchemaLike<TOutput>>(schema: TSchema, options?: YupAdapterOptions) => ReturnType<typeof createBodyValidator>;
28
+ /**
29
+ * Builds middleware that validates `req.params` with Yup.
30
+ */
31
+ export declare const yupParamsValidator: <TOutput, TSchema extends YupSchemaLike<TOutput>>(schema: TSchema, options?: YupAdapterOptions) => ReturnType<typeof createParamsValidator>;
32
+ /**
33
+ * Builds middleware that validates `req.query` with Yup.
34
+ */
35
+ export declare const yupQueryValidator: <TOutput, TSchema extends YupSchemaLike<TOutput>>(schema: TSchema, options?: YupAdapterOptions) => ReturnType<typeof createQueryValidator>;
36
+ export {};
@@ -0,0 +1,23 @@
1
+ import { type ZodTypeAny, z } from "zod";
2
+ import type { ValidationAdapter } from "../types";
3
+ import { createBodyValidator, createParamsValidator, createQueryValidator, createValidatorMiddleware } from "../validator";
4
+ /**
5
+ * Creates a Zod-backed validation adapter.
6
+ */
7
+ export declare const createZodAdapter: <TSchema extends ZodTypeAny>(schema: TSchema) => ValidationAdapter<unknown, z.output<TSchema>>;
8
+ /**
9
+ * Builds middleware for any request target using a Zod schema.
10
+ */
11
+ export declare const zodMiddleware: <TSchema extends ZodTypeAny>(schema: TSchema, options?: Parameters<typeof createValidatorMiddleware>[1]) => ReturnType<typeof createValidatorMiddleware>;
12
+ /**
13
+ * Builds middleware that validates `req.body` with Zod.
14
+ */
15
+ export declare const zodBodyValidator: <TSchema extends ZodTypeAny>(schema: TSchema, options?: Parameters<typeof createBodyValidator>[1]) => ReturnType<typeof createBodyValidator>;
16
+ /**
17
+ * Builds middleware that validates `req.params` with Zod.
18
+ */
19
+ export declare const zodParamsValidator: <TSchema extends ZodTypeAny>(schema: TSchema, options?: Parameters<typeof createParamsValidator>[1]) => ReturnType<typeof createParamsValidator>;
20
+ /**
21
+ * Builds middleware that validates `req.query` with Zod.
22
+ */
23
+ export declare const zodQueryValidator: <TSchema extends ZodTypeAny>(schema: TSchema, options?: Parameters<typeof createQueryValidator>[1]) => ReturnType<typeof createQueryValidator>;
@@ -0,0 +1,19 @@
1
+ import { plainToInstance } from "class-transformer";
2
+ import { resolveValidationMessage } from "../config.js";
3
+ import { createBodyValidator, createParamsValidator, createQueryValidator, createValidationAdapter, createValidatorMiddleware } from "../validator.js";
4
+ const defaultFailure = (error, message)=>({
5
+ message,
6
+ issues: error instanceof Error ? error.message : error
7
+ });
8
+ const createClassTransformerAdapter = (target, options = {})=>createValidationAdapter((input)=>{
9
+ const value = plainToInstance(target, input, options);
10
+ const message = resolveValidationMessage(options.message);
11
+ if (Array.isArray(value)) throw new Error(message);
12
+ if (options.validate && !options.validate(value)) throw new Error(message);
13
+ return value;
14
+ }, (error)=>defaultFailure(error, resolveValidationMessage(options.message)));
15
+ const classTransformerMiddleware = (target, options = {})=>createValidatorMiddleware(createClassTransformerAdapter(target, options));
16
+ const classTransformerBodyValidator = (target, options = {})=>createBodyValidator(createClassTransformerAdapter(target, options));
17
+ const classTransformerParamsValidator = (target, options = {})=>createParamsValidator(createClassTransformerAdapter(target, options));
18
+ const classTransformerQueryValidator = (target, options = {})=>createQueryValidator(createClassTransformerAdapter(target, options));
19
+ export { classTransformerBodyValidator, classTransformerMiddleware, classTransformerParamsValidator, classTransformerQueryValidator, createClassTransformerAdapter };
@@ -0,0 +1,24 @@
1
+ export declare const DEFAULT_VALIDATION_MESSAGE = "Validation failed";
2
+ /**
3
+ * Global configuration shape for Nexva consumers.
4
+ */
5
+ export type ExpressuConfig = Partial<{
6
+ validationMessage: string;
7
+ }>;
8
+ /**
9
+ * Updates the global Nexva configuration.
10
+ */
11
+ export declare const configure: (config?: ExpressuConfig) => void;
12
+ /**
13
+ * Resets the global Nexva configuration to defaults.
14
+ */
15
+ export declare const resetConfig: () => void;
16
+ /**
17
+ * Returns the current global Nexva configuration.
18
+ */
19
+ export declare const getConfig: () => Readonly<ExpressuConfig>;
20
+ /**
21
+ * Resolves the validation message using the provided override, the global config,
22
+ * and the package default.
23
+ */
24
+ export declare const resolveValidationMessage: (message?: string) => string;
@@ -0,0 +1,3 @@
1
+ export * from "./config";
2
+ export * from "./types";
3
+ export * from "./validator";
package/dist/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./config.js";
2
+ export * from "./types.js";
3
+ export * from "./validator.js";
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Supported request targets that validators can replace.
3
+ */
4
+ export type ValidationTarget = "body" | "params" | "query";
5
+ /**
6
+ * Standard validation failure payload returned by middleware.
7
+ */
8
+ export type ValidationFailure = {
9
+ message: string;
10
+ } & Partial<{
11
+ issues: unknown;
12
+ }>;
13
+ /**
14
+ * Generic validation adapter contract for parsing request payloads.
15
+ */
16
+ export type ValidationAdapter<TInput = unknown, TOutput = TInput> = {
17
+ parse(input: TInput): TOutput;
18
+ } & Partial<{
19
+ toFailure: (error: unknown) => ValidationFailure;
20
+ }>;
21
+ /**
22
+ * Common middleware options for validation builders.
23
+ */
24
+ export type MiddlewareOptions = Partial<{
25
+ target: ValidationTarget;
26
+ statusCode: number;
27
+ message: string;
28
+ }>;
@@ -0,0 +1,22 @@
1
+ import type { RequestHandler } from "express";
2
+ import type { MiddlewareOptions, ValidationAdapter, ValidationFailure } from "./types";
3
+ /**
4
+ * Creates a validation adapter from a parse function and an optional error mapper.
5
+ */
6
+ export declare const createValidationAdapter: <TInput, TOutput>(parse: (input: TInput) => TOutput, toFailure?: (error: unknown) => ValidationFailure) => ValidationAdapter<TInput, TOutput>;
7
+ /**
8
+ * Builds Express middleware that validates and replaces a request target.
9
+ */
10
+ export declare const createValidatorMiddleware: <TInput, TOutput>(adapter: ValidationAdapter<TInput, TOutput>, options?: MiddlewareOptions) => RequestHandler;
11
+ /**
12
+ * Builds middleware that validates `req.body`.
13
+ */
14
+ export declare const createBodyValidator: <TInput, TOutput>(adapter: ValidationAdapter<TInput, TOutput>, options?: Omit<MiddlewareOptions, "target">) => RequestHandler;
15
+ /**
16
+ * Builds middleware that validates `req.params`.
17
+ */
18
+ export declare const createParamsValidator: <TInput, TOutput>(adapter: ValidationAdapter<TInput, TOutput>, options?: Omit<MiddlewareOptions, "target">) => RequestHandler;
19
+ /**
20
+ * Builds middleware that validates `req.query`.
21
+ */
22
+ export declare const createQueryValidator: <TInput, TOutput>(adapter: ValidationAdapter<TInput, TOutput>, options?: Omit<MiddlewareOptions, "target">) => RequestHandler;
package/dist/yup.js ADDED
@@ -0,0 +1,23 @@
1
+ import { ValidationError } from "yup";
2
+ import { resolveValidationMessage } from "../config.js";
3
+ import { createBodyValidator, createParamsValidator, createQueryValidator, createValidationAdapter, createValidatorMiddleware } from "../validator.js";
4
+ const defaultFailure = (error, message)=>({
5
+ message,
6
+ issues: error instanceof Error ? error.message : error
7
+ });
8
+ const createYupAdapter = (schema, options = {})=>createValidationAdapter((input)=>schema.validateSync(input, options), (error)=>{
9
+ if (error instanceof ValidationError) return {
10
+ message: resolveValidationMessage(options.message),
11
+ issues: {
12
+ errors: error.errors,
13
+ inner: error.inner,
14
+ path: error.path
15
+ }
16
+ };
17
+ return defaultFailure(error, resolveValidationMessage(options.message));
18
+ });
19
+ const yupMiddleware = (schema, options = {})=>createValidatorMiddleware(createYupAdapter(schema, options));
20
+ const yupBodyValidator = (schema, options = {})=>createBodyValidator(createYupAdapter(schema, options));
21
+ const yupParamsValidator = (schema, options = {})=>createParamsValidator(createYupAdapter(schema, options));
22
+ const yupQueryValidator = (schema, options = {})=>createQueryValidator(createYupAdapter(schema, options));
23
+ export { createYupAdapter, yupBodyValidator, yupMiddleware, yupParamsValidator, yupQueryValidator };
package/dist/zod.js ADDED
@@ -0,0 +1,18 @@
1
+ import { z } from "zod";
2
+ import { resolveValidationMessage } from "../config.js";
3
+ import { createBodyValidator, createParamsValidator, createQueryValidator, createValidationAdapter, createValidatorMiddleware } from "../validator.js";
4
+ const createZodAdapter = (schema)=>createValidationAdapter((input)=>schema.parse(input), (error)=>{
5
+ if (error instanceof z.ZodError) return {
6
+ message: resolveValidationMessage(),
7
+ issues: error.issues
8
+ };
9
+ return {
10
+ message: resolveValidationMessage(),
11
+ issues: error
12
+ };
13
+ });
14
+ const zodMiddleware = (schema, options = {})=>createValidatorMiddleware(createZodAdapter(schema), options);
15
+ const zodBodyValidator = (schema, options = {})=>createBodyValidator(createZodAdapter(schema), options);
16
+ const zodParamsValidator = (schema, options = {})=>createParamsValidator(createZodAdapter(schema), options);
17
+ const zodQueryValidator = (schema, options = {})=>createQueryValidator(createZodAdapter(schema), options);
18
+ export { createZodAdapter, zodBodyValidator, zodMiddleware, zodParamsValidator, zodQueryValidator };
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "@nathan3boss/expressu",
3
+ "version": "0.1.0",
4
+ "description": "Composable middleware builders for Express validators",
5
+ "license": "MIT",
6
+ "author": "nathan2slime",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/nathan2slime/expressu.git"
10
+ },
11
+ "homepage": "https://github.com/nathan2slime/expressu#readme",
12
+ "bugs": {
13
+ "url": "https://github.com/nathan2slime/expressu/issues"
14
+ },
15
+ "publishConfig": {
16
+ "access": "public"
17
+ },
18
+ "type": "module",
19
+ "packageManager": "pnpm@10.30.3",
20
+ "main": "./dist/index.js",
21
+ "types": "./dist/index.d.ts",
22
+ "exports": {
23
+ ".": {
24
+ "types": "./dist/index.d.ts",
25
+ "import": "./dist/index.js"
26
+ },
27
+ "./zod": {
28
+ "types": "./dist/adapters/zod.d.ts",
29
+ "import": "./dist/adapters/zod.js"
30
+ },
31
+ "./yup": {
32
+ "types": "./dist/adapters/yup.d.ts",
33
+ "import": "./dist/adapters/yup.js"
34
+ },
35
+ "./class-transformer": {
36
+ "types": "./dist/adapters/class-transformer.d.ts",
37
+ "import": "./dist/adapters/class-transformer.js"
38
+ }
39
+ },
40
+ "files": [
41
+ "dist",
42
+ "README.md"
43
+ ],
44
+ "scripts": {
45
+ "build": "rslib",
46
+ "dev": "rslib --watch",
47
+ "typecheck": "tsc --noEmit -p tsconfig.json",
48
+ "lint": "biome check .",
49
+ "format": "biome format --write .",
50
+ "test": "rstest --globals",
51
+ "test:coverage": "rstest --globals --coverage",
52
+ "changeset": "changeset",
53
+ "version-packages": "changeset version",
54
+ "release": "changeset publish",
55
+ "prepare": "lefthook install"
56
+ },
57
+ "peerDependencies": {
58
+ "class-transformer": "^0.5.1",
59
+ "express": "^5.2.1",
60
+ "yup": "^1.6.1",
61
+ "zod": "^4.3.6"
62
+ },
63
+ "peerDependenciesMeta": {
64
+ "class-transformer": {
65
+ "optional": true
66
+ },
67
+ "yup": {
68
+ "optional": true
69
+ },
70
+ "zod": {
71
+ "optional": true
72
+ }
73
+ },
74
+ "devDependencies": {
75
+ "@biomejs/biome": "^2.0.0",
76
+ "@changesets/cli": "^2.29.0",
77
+ "@commitlint/cli": "^19.0.0",
78
+ "@commitlint/config-conventional": "^19.0.0",
79
+ "@rslib/core": "^0.20.0",
80
+ "@rstest/core": "^0.9.6",
81
+ "@rstest/coverage-istanbul": "^0.3.1",
82
+ "class-transformer": "^0.5.1",
83
+ "@types/express": "^5.0.6",
84
+ "@types/node": "^24.12.2",
85
+ "lefthook": "^1.10.10",
86
+ "typescript": "^5.9.2",
87
+ "yup": "^1.6.1",
88
+ "zod": "^4.3.6"
89
+ }
90
+ }