Skip to content

Commit eccab3d

Browse files
committed
make custom validator as optional
1 parent 96e57b1 commit eccab3d

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

src/auth/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ export default {
77
}).unknown(true),
88
auth: Joi.object().keys({
99
'x-access-token': Joi.string().required().min(1),
10-
'x-user-id': JoiObjectId(),
10+
'x-user-id': JoiObjectId().required(),
1111
}).unknown(true)
1212
};

src/helpers/validator.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,16 @@ export enum ValidationSource {
1111
PARAM = 'params'
1212
}
1313

14-
export const JoiObjectId = () => Joi.string().required().custom((value: string, helpers) => {
15-
if (!Types.ObjectId.isValid(value))
16-
return helpers.error('any.invalid');
14+
export const JoiObjectId = () => Joi.string().custom((value: string, helpers) => {
15+
if (!Types.ObjectId.isValid(value)) return helpers.error('any.invalid');
1716
return value;
1817
}, 'Object Id Validation');
1918

19+
export const JoiUrlEndpoint = () => Joi.string().custom((value: string, helpers) => {
20+
if (!value.startsWith('/')) return helpers.error('any.invalid');
21+
return value;
22+
}, 'Url Endpoint Validation');
23+
2024

2125
export default (schema: Joi.ObjectSchema, source: ValidationSource = ValidationSource.BODY) =>
2226
(req: Request, res: Response, next: NextFunction) => {

src/routes/v1/access/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
}),
1212
auth: Joi.object().keys({
1313
'x-access-token': Joi.string().required().min(1),
14-
'x-user-id': JoiObjectId(),
14+
'x-user-id': JoiObjectId().required(),
1515
}).unknown(true),
1616
signup: Joi.object().keys({
1717
name: Joi.string().required().min(3),

src/routes/v1/blog/schema.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import Joi from '@hapi/joi';
2-
import { JoiObjectId } from '../../../helpers/validator';
2+
import { JoiObjectId, JoiUrlEndpoint } from '../../../helpers/validator';
33

44
export default {
55
blogUrl: Joi.object().keys({
66
url: Joi.string().required().uri()
77
}),
88
blogId: Joi.object().keys({
9-
id: JoiObjectId()
9+
id: JoiObjectId().required()
1010
}),
1111
blogTag: Joi.object().keys({
1212
tag: Joi.string().required().min(1)
@@ -16,13 +16,13 @@ export default {
1616
pageItemCount: Joi.number().required().integer().min(1),
1717
}),
1818
authorId: Joi.object().keys({
19-
id: JoiObjectId()
19+
id: JoiObjectId().required()
2020
}),
2121
blogCreate: Joi.object().keys({
2222
title: Joi.string().required().min(3).max(500),
2323
description: Joi.string().required().min(3).max(2000),
2424
text: Joi.string().required().max(50000),
25-
blogUrl: Joi.string().required().uri().max(200),
25+
blogUrl: JoiUrlEndpoint().required().max(200),
2626
imgUrl: Joi.string().optional().uri().max(200),
2727
score: Joi.number().optional().min(0).max(1),
2828
tags: Joi.array().optional().items(Joi.string().uppercase().min(1)),
@@ -31,7 +31,7 @@ export default {
3131
title: Joi.string().optional().min(3).max(500),
3232
description: Joi.string().optional().min(3).max(2000),
3333
text: Joi.string().optional().max(50000),
34-
blogUrl: Joi.string().optional().uri().max(200),
34+
blogUrl: JoiUrlEndpoint().required().max(200),
3535
imgUrl: Joi.string().optional().uri().max(200),
3636
score: Joi.number().optional().min(0).max(1),
3737
tags: Joi.array().optional().items(Joi.string().uppercase().min(1))

src/routes/v1/profile/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { JoiObjectId } from '../../../helpers/validator';
33

44
export default {
55
userId: Joi.object().keys({
6-
id: JoiObjectId()
6+
id: JoiObjectId().required()
77
}),
88
profile: Joi.object().keys({
99
name: Joi.string().optional().min(1).max(200),

0 commit comments

Comments
 (0)