Initial commit: add .gitignore and README
This commit is contained in:
28
packages/validation/package.json
Normal file
28
packages/validation/package.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "@workspace/validation",
|
||||
"version": "1.0.0",
|
||||
"description": "Zod schemas and validators",
|
||||
"main": "./dist/index.js",
|
||||
"types": "./dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test": "vitest",
|
||||
"lint": "eslint src",
|
||||
"type-check": "tsc --noEmit",
|
||||
"clean": "rm -rf dist"
|
||||
},
|
||||
"dependencies": {
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.5.4",
|
||||
"vitest": "^1.2.0"
|
||||
},
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"publishConfig": {
|
||||
"access": "restricted"
|
||||
}
|
||||
}
|
||||
|
||||
8
packages/validation/src/index.ts
Normal file
8
packages/validation/src/index.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @workspace/validation
|
||||
* Zod schemas and validators
|
||||
*/
|
||||
|
||||
export * from './schemas';
|
||||
export * from './validators';
|
||||
|
||||
37
packages/validation/src/schemas.ts
Normal file
37
packages/validation/src/schemas.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Common validation schemas
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Email validation schema
|
||||
*/
|
||||
export const emailSchema = z.string().email('Invalid email address');
|
||||
|
||||
/**
|
||||
* UUID validation schema
|
||||
*/
|
||||
export const uuidSchema = z.string().uuid('Invalid UUID');
|
||||
|
||||
/**
|
||||
* URL validation schema
|
||||
*/
|
||||
export const urlSchema = z.string().url('Invalid URL');
|
||||
|
||||
/**
|
||||
* Pagination schema
|
||||
*/
|
||||
export const paginationSchema = z.object({
|
||||
page: z.number().int().positive().default(1),
|
||||
limit: z.number().int().positive().max(100).default(10),
|
||||
});
|
||||
|
||||
/**
|
||||
* Sort schema
|
||||
*/
|
||||
export const sortSchema = z.object({
|
||||
field: z.string(),
|
||||
order: z.enum(['asc', 'desc']).default('asc'),
|
||||
});
|
||||
|
||||
27
packages/validation/src/validators.ts
Normal file
27
packages/validation/src/validators.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
/**
|
||||
* Validation utilities
|
||||
*/
|
||||
|
||||
import { z } from 'zod';
|
||||
|
||||
/**
|
||||
* Validate data against schema
|
||||
*/
|
||||
export function validate<T>(schema: z.ZodSchema<T>, data: unknown): T {
|
||||
return schema.parse(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Safe validate (returns result instead of throwing)
|
||||
*/
|
||||
export function validateSafe<T>(
|
||||
schema: z.ZodSchema<T>,
|
||||
data: unknown
|
||||
): { success: true; data: T } | { success: false; error: z.ZodError } {
|
||||
const result = schema.safeParse(data);
|
||||
if (result.success) {
|
||||
return { success: true, data: result.data };
|
||||
}
|
||||
return { success: false, error: result.error };
|
||||
}
|
||||
|
||||
20
packages/validation/tsconfig.json
Normal file
20
packages/validation/tsconfig.json
Normal file
@@ -0,0 +1,20 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"lib": ["ES2022"],
|
||||
"declaration": true,
|
||||
"declarationMap": true,
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"moduleResolution": "node",
|
||||
"resolveJsonModule": true
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user