Archived
2
This repository has been archived on 2023-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
definma-api/src/routes/validate/id.ts

26 lines
612 B
TypeScript
Raw Normal View History

2020-05-07 21:55:29 +02:00
import Joi from '@hapi/joi';
2020-04-29 12:10:27 +02:00
export default class IdValidate {
2020-05-07 21:55:29 +02:00
private static id = Joi.string().pattern(new RegExp('[0-9a-f]{24}')).length(24);
2020-04-29 12:10:27 +02:00
static get () {
return this.id;
}
static valid (id) {
return this.id.validate(id).error === undefined;
}
2020-05-06 14:39:04 +02:00
static parameter () { // :id url parameter
2020-04-29 12:10:27 +02:00
return ':id([0-9a-f]{24})';
}
2020-05-06 14:39:04 +02:00
static stringify (data) {
Object.keys(data).forEach(key => {
if (data[key] !== null && data[key].hasOwnProperty('_bsontype') && data[key]._bsontype === 'ObjectID') {
data[key] = data[key].toString();
}
});
return data;
}
2020-04-29 12:10:27 +02:00
}