2
0
mirror of https://github.com/frappe/books.git synced 2024-09-20 03:29:00 +00:00

feat: validate method for fields

This commit is contained in:
Faris Ansari 2019-12-20 11:52:16 +05:30
parent 3a1a4caa78
commit 2205a212eb
2 changed files with 12 additions and 4 deletions

View File

@ -67,6 +67,7 @@ class CannotCommitError extends DatabaseError {
class ValueError extends ValidationError {}
class Conflict extends ValidationError {}
class InvalidFieldError extends ValidationError {}
function throwError(message, error = 'ValidationError') {
const errorClass = {
@ -95,5 +96,6 @@ module.exports = {
DatabaseError,
CannotCommitError,
MandatoryError,
InvalidFieldError,
throw: throwError
};

View File

@ -79,7 +79,8 @@ module.exports = class BaseDocument extends Observable {
this.append(fieldname, row);
}
} else {
this[fieldname] = await this.validateField(fieldname, value);
await this.validateField(fieldname, value);
this[fieldname] = value;
}
// always run applyChange from the parentdoc
@ -197,10 +198,15 @@ module.exports = class BaseDocument extends Observable {
async validateField(key, value) {
let field = this.meta.getField(key);
if (field && field.fieldtype == 'Select') {
return this.meta.validateSelect(field, value);
if (!field) {
throw new frappe.errors.InvalidFieldError(`Invalid field ${key}`);
}
if (field.fieldtype == 'Select') {
this.meta.validateSelect(field, value);
}
if (field.validate) {
await field.validate(value, this);
}
return value;
}
getValidDict() {