README
vados-validation
A plugin inject function help validates model before using vados
Install
npm i vados-validation -S
Examples
Typescript
File service
import { VALIDATE, Checker } from 'vados-validation';
import { MONGO, Mongo, Uuid, Collection } from 'vados-mongo';
export class ChartService {
@MONGO()
static mongo: Mongo;
@VALIDATE((body: Chart) => {
body._id = <Uuid>Mongo.uuid();
Checker.must('project_id', body.project_id, Uuid);
Checker.must('account_id', body.account_id, Uuid);
Checker.must('page_id', body.page_id, Uuid);
Checker.must('oder', body.oder, Number, 1);
Checker.must('name', body.name, String);
Checker.option('des', body.des, String);
Checker.must('options', body.options, Object);
body.created_at = new Date();
body.updated_at = new Date();
})
static async insert(body: Chart, validate?: Function): Promise<Chart> {
// Auto call validate
const rs: Chart = await ChartService.mongo.insert<Chart>(Chart, body);
return rs;
}
@VALIDATE((body: Chart) => {
body._id = <Uuid>Mongo.uuid();
Checker.must('project_id', body.project_id, Uuid);
Checker.must('account_id', body.account_id, Uuid);
Checker.must('page_id', body.page_id, Uuid);
Checker.must('oder', body.oder, Number, 1);
Checker.must('name', body.name, String);
Checker.option('des', body.des, String);
Checker.must('options', body.options, Object);
body.created_at = new Date();
body.updated_at = new Date();
}, false)
static async insertManual(body: Chart, validate?: Function): Promise<Chart> {
validate(body); // Customize call validate
const rs: Chart = await ChartService.mongo.insert<Chart>(Chart, body);
return rs;
}
}