Skip to content

Commit f0eada2

Browse files
📚 docs(Validation): Custom validation docs added
1 parent a002e52 commit f0eada2

1 file changed

Lines changed: 67 additions & 7 deletions

File tree

docs/endpoints/data-validation.md

Lines changed: 67 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { Yelix, requestDataValidationYelixMiddleware } from 'jsr:@murat/yelix';
1515

1616
export async function startServer() {
1717
const app = new Yelix();
18+
19+
// Load your endpoints here
20+
1821
app.setMiddleware('dataValidation', requestDataValidationYelixMiddleware);
1922
app.serve();
2023
}
@@ -27,20 +30,19 @@ await startServer();
2730
```ts title="hello.ts"
2831
import { Ctx, ValidationType, inp } from "jsr:@murat/yelix";
2932

30-
export async function POST(ctx: Ctx) {
33+
export async function GET(ctx: Ctx) {
3134
const requestData = ctx.get('dataValidation').user;
32-
const { username, email } = requestData.body;
33-
return await ctx.text(`Hello, ${username}!`, 200);
35+
const { name } = requestData.query;
36+
return await ctx.text(`Hello, ${name}!`, 200);
3437
}
3538

3639
export const path = '/api/hello';
3740
export const middlewares = ['dataValidation'];
3841

3942
export const validation: ValidationType = {
40-
body: inp().object({
41-
username: inp().string().min(3).max(255),
42-
email: inp().string().email()
43-
})
43+
query: {
44+
name: inp().string().min(3).max(255)
45+
}
4446
};
4547
```
4648

@@ -149,6 +151,64 @@ inp().file()
149151
.optional() // Make field optional
150152
```
151153

154+
### Custom Validations
155+
```ts title="validation/superString.ts"
156+
// deno-lint-ignore-file no-explicit-any
157+
import { FailedMessage, StringZod, YelixInput } from '@murat/yelix';
158+
159+
class SuperString extends StringZod {
160+
override input: YelixInput;
161+
162+
constructor() {
163+
const input = new YelixInput();
164+
super(input);
165+
166+
this.input = input;
167+
}
168+
169+
isUserID(failedMessage?: FailedMessage): this {
170+
this.addRule(
171+
'isUserID',
172+
null,
173+
(value: any) => ({
174+
isOk:
175+
value && typeof value === 'string'
176+
? value.startsWith('user_')
177+
: false,
178+
}),
179+
failedMessage
180+
? failedMessage
181+
: 'User ID must start with "user_"',
182+
);
183+
return this;
184+
}
185+
}
186+
187+
const superString = () => new SuperString();
188+
189+
export { SuperString, superString };
190+
```
191+
192+
```ts title="hello.ts"
193+
import { Ctx, ValidationType } from "jsr:@murat/yelix";
194+
import { superString } from "../validation/customValidation.ts";
195+
196+
export async function GET(ctx: Ctx) {
197+
const requestData = ctx.get('dataValidation').user;
198+
const { name } = requestData.query;
199+
return await ctx.text(`Hello, ${name}!`, 200);
200+
}
201+
202+
export const path = '/api/customValidation';
203+
export const middlewares = ['dataValidation'];
204+
205+
export const validation: ValidationType = {
206+
query: {
207+
userId: superString().trim().isUserID(),
208+
}
209+
};
210+
```
211+
152212
## Complete Example
153213

154214
```ts

0 commit comments

Comments
 (0)