mirror of
https://github.com/frappe/books.git
synced 2025-01-23 15:18:24 +00:00
feat: validate method for fields
This commit is contained in:
parent
3a1a4caa78
commit
2205a212eb
@ -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
|
||||
};
|
||||
|
@ -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() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user