2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +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 ValueError extends ValidationError {}
class Conflict extends ValidationError {} class Conflict extends ValidationError {}
class InvalidFieldError extends ValidationError {}
function throwError(message, error = 'ValidationError') { function throwError(message, error = 'ValidationError') {
const errorClass = { const errorClass = {
@ -95,5 +96,6 @@ module.exports = {
DatabaseError, DatabaseError,
CannotCommitError, CannotCommitError,
MandatoryError, MandatoryError,
InvalidFieldError,
throw: throwError throw: throwError
}; };

View File

@ -79,7 +79,8 @@ module.exports = class BaseDocument extends Observable {
this.append(fieldname, row); this.append(fieldname, row);
} }
} else { } else {
this[fieldname] = await this.validateField(fieldname, value); await this.validateField(fieldname, value);
this[fieldname] = value;
} }
// always run applyChange from the parentdoc // always run applyChange from the parentdoc
@ -197,10 +198,15 @@ module.exports = class BaseDocument extends Observable {
async validateField(key, value) { async validateField(key, value) {
let field = this.meta.getField(key); let field = this.meta.getField(key);
if (field && field.fieldtype == 'Select') { if (!field) {
return this.meta.validateSelect(field, value); 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() { getValidDict() {