diff --git a/common/errors.js b/common/errors.js index 713427e5..9771c444 100644 --- a/common/errors.js +++ b/common/errors.js @@ -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 }; diff --git a/model/document.js b/model/document.js index 5f30091f..eaf5bf61 100644 --- a/model/document.js +++ b/model/document.js @@ -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() {