mirror of
https://iceshrimp.dev/iceshrimp/iceshrimp
synced 2024-12-02 00:38:08 +09:00
Custom validator support
This commit is contained in:
parent
c2e7829f2b
commit
9a122f764e
@ -2,7 +2,7 @@ import * as mongo from 'mongodb';
|
||||
|
||||
type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object';
|
||||
|
||||
export default <T>(value: any, isRequired: boolean, type: Type): [T, string] => {
|
||||
export default <T>(value: any, isRequired: boolean, type: Type, validator?: (any) => boolean): [T, string] => {
|
||||
if (value === undefined || value === null) {
|
||||
if (isRequired) {
|
||||
return [null, 'is-required']
|
||||
@ -49,5 +49,11 @@ export default <T>(value: any, isRequired: boolean, type: Type): [T, string] =>
|
||||
break;
|
||||
}
|
||||
|
||||
if (validator) {
|
||||
if (!validator(value)) {
|
||||
return [null, 'invalid-format'];
|
||||
}
|
||||
}
|
||||
|
||||
return [value, null];
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user