本文整理汇总了TypeScript中validate.js.default方法的典型用法代码示例。如果您正苦于以下问题:TypeScript js.default方法的具体用法?TypeScript js.default怎么用?TypeScript js.default使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类validate.js
的用法示例。
在下文中一共展示了js.default方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: function
export default function(model) {
return validate(model, validationSpec);
};
示例2: validateRequest
// res: Response where to send error HTTP message if the request is invalid
// requestBody: req.body OR req.query
// constraints: validate.js format
// return true if the request is valid
static validateRequest(res: Response,
requestBody: any,
constraints: any): boolean {
console.log(requestBody);
if (_.isEmpty(requestBody)) {
APIHelper.sendEmptyRequestResponse(res);
return false;
}
let validation = validate(requestBody, constraints);
console.log(requestBody);
if (_.keys(validation).length > 0) {
let validationErrors = {};
for (let prop in validation) {
if (!validation.hasOwnProperty(prop)) {
continue;
}
validationErrors[prop] = validation[prop];
}
APIHelper.sendInvalidInputResponse(res, validationErrors);
return false;
} else {
return true;
}
}
示例3: bookCreateOrUpdate
function bookCreateOrUpdate(req: Request, res: Response,
update: boolean) {
let renderObject = {};
let currentYear = new Date().getFullYear();
let constraints = {
title: {
presence: true,
},
isbn: {
presence: update ? false : true
},
author: {
presence: true
},
pages: {
presence: true,
numericality: {
onlyInteger: true,
greaterThan: 0,
}
},
year: {
presence: true,
numericality: {
onlyInteger: true,
lessThan: currentYear,
notLessThan: "must be in the past."
}
},
editorid: {
presence: true,
numericality: {
onlyInteger: true,
}
}
};
res.locals.errors = {};
let book: Book = new Book();
let validation = validate(req.body, constraints);
let wrongInputs = 0;
for (let prop in validation) {
if (!validation.hasOwnProperty(prop)) {
continue;
}
res.locals.errors[prop] = validation[prop];
delete req.body[prop];
wrongInputs++;
}
for (let prop in req.body) {
if (!req.body.hasOwnProperty(prop)) {
continue;
}
book[prop] = req.body[prop];
}
res.locals.info = {};
if (wrongInputs === 0) {
if (update) {
Book.dao.update(book, function (err) {
if (err) {
res.locals.errors.global = err;
} else {
res.locals.info.confirmation = "The book has been updated.";
}
});
} else {
Book.dao.create(book, function (err) {
if (err) {
res.locals.errors.global = err;
} else {
res.locals.info.confirmation = "The book has been created.";
}
});
}
}
res.render("book/create", {
book: book,
update: update
});
}
示例4: bookSearchGET
function bookSearchGET(req: Request, res: Response, next: NextFunction) {
if (Object.keys(req.query).length === 0 && req.baseUrl !== "/api") {
res.render("book/search", {data: {}});
}
else {
let constraints = {
title: {
presence: false,
},
isbn: {
presence: false
},
author: {
presence: false
},
pages: {
presence: false,
numericality: {
onlyInteger: true,
greaterThan: 0,
}
},
year: {
presence: false,
numericality: {
onlyInteger: true,
}
},
editorid: {
presence: false,
numericality: {
onlyInteger: true,
}
}
};
let book: Book = new Book();
let validation = validate(req.query, constraints);
let data = {
errors: {global: ""},
info: {confirmation: ""},
books: [],
book: {}
};
let wrongInputs = 0;
for (let prop in validation) {
if (!validation.hasOwnProperty(prop)) {
continue;
}
data.errors[prop] = validation[prop];
delete req.query[prop];
wrongInputs++;
}
for (let prop in req.query) {
if (!req.query.hasOwnProperty(prop)) {
continue;
}
book[prop] = req.query[prop];
}
if (wrongInputs === 0) {
let whereQuery = _.omit(_.pick(req.query, "pages", "year", "editorid"),
function (value, key, object) {
return value === "";
});
let likeQuery_ = _.chain(req.query)
.pick(req.query, "title", "isbn", "author")
.omit(function (value, key, object) {
return value === "";
}).value();
let likeQuery = _.mapObject(likeQuery_, function (value, key) {
return "%" + value + "%";
});
console.log("Like query=");
console.log(likeQuery);
Book.dao.find({
where: whereQuery, like: likeQuery
}, function (err, books) {
if (err) {
data.errors.global = err.code;
} else {
data.books = books;
let asyncToDo = data.books.length;
_.each(data.books, function (value, index, list) {
Editor.dao.find({where: {id: list[index].editorid}}, function (err, editors) {
list[index].editor = editors[0];
asyncToDo--;
if (asyncToDo === 0) {
data.info.confirmation = books.length + " books found.";
data.book = req.query;
if (req.baseUrl === "/api") {
res.json(data);
return;
}
if (req.xhr) {
//.........这里部分代码省略.........
示例5: validation
function validation(obj: any, constraint, options?: any ) {
return validateJs(obj, constraint, options)
}
示例6: function
const updateState = (state: RowsFormProps, action: UpdateRowAction) => {
const newState = { ...state };
for (const idx in newState.rows) {
let row = newState.rows[idx];
if (row.id === action.row.id) {
row = { ...row, ...action.row };
validate.extend(validate.validators.datetime, {
parse: function(value: any) {
return +moment.utc(value);
},
format: function(value: any, options: any) {
const format = options.dateOnly
? 'YYYY-MM-DD'
: 'YYYY-MM-DD hh:mm:ss';
return moment.utc(value).format(format);
}
});
row.validReason = validate(
row,
{
date: {
presence: {
message: 'må være en gyldig dato som ikke er i fremtid'
},
date: {
earliest: moment()
.subtract(1, 'years')
.format('YYYY-MM-DD'),
latest: moment().format('YYYY-MM-DD'),
message: 'må være en gyldig dato som ikke er i fremtid'
}
},
description: {
presence: { message: 'må være minst 2 tegn' },
length: { minimum: 2, message: 'må være minst 2 tegn' }
},
supplier: {
presence: { message: 'må være minst 2 tegn' },
length: { minimum: 2, message: 'må være minst 2 tegn' }
},
cost: {
presence: { message: 'må være + NOK' },
numericality: { greaterThan: 0, message: 'må være + NOK' }
}
},
{ fullMessages: false }
);
row.valid = row.validReason === undefined;
if (row.valid) {
row.dirty = false;
}
newState.rows[idx] = row;
}
if (row.valid) {
const cost = getCost(row);
// Force to number
row.cost = cost;
}
}
newState.total = calculateSum(newState.rows);
return newState;
};
示例7: editorSearchPartialGET
function editorSearchPartialGET(req: Request, res: Response, next: NextFunction) {
let data = {
errors: {global: ""},
info: {confirmation: ""},
editors: {},
// Clone the search form to send it back
editorSearchForm: req.query,
layout: !req.xhr
};
if (Object.keys(req.query).length === 0) {
res.render("partials/book/editorSearchDiv", data);
}
else {
// Data to send back to the browser
// Constraint applied to the query
let constraints = {
city: {
presence: false
},
name: {
presence: false
},
id: {
presence: false,
numericality: {
onlyInteger: true,
}
}
};
let validation = validate(req.query, constraints);
let wrongInputs = 0;
// Find invalid values in the query
for (let prop in validation) {
if (!validation.hasOwnProperty(prop)) {
continue;
}
data.errors[prop] = validation[prop];
wrongInputs++;
}
// If there is no invalid values in the query, search for the editor
// in the database and send the data
if (wrongInputs === 0) {
// Remove empty strings before searching
// (else it would select "where city = "", name = """ etc.
//NOTE: _ doesn't work if it's not imported and will fail silently...
let whereQuery = _.omit(req.query, function(value, key, object) {
return value === "";
});
Editor.dao.find({
where: whereQuery
}, function (err, editors) {
if (err) {
// TODO.md: convert database error codes to human-readable errors
data.errors.global = err.code;
} else {
data.info.confirmation = editors.length + " editors found.";
data.editors = editors;
}
console.log(data);
res.render("partials/book/editorSearchDiv", data);
});
} else {
res.render("partials/book/editorSearchDiv", data);
}
}
}