2
0
mirror of https://github.com/frappe/books.git synced 2024-12-26 12:28:12 +00:00

fix: Validate doctype fields

This commit is contained in:
Faris Ansari 2020-01-28 16:26:29 +05:30
parent ce5ecd1c18
commit 0a401d32cd

View File

@ -153,13 +153,27 @@ module.exports = class BaseMeta extends BaseDocument {
this._validFieldsWithChildren.push(field); this._validFieldsWithChildren.push(field);
}; };
const doctype_fields = this.fields.map(field => field.fieldname); // fields validation
this.fields.forEach((df, i) => {
if (!df.fieldname) {
throw new frappe.errors.ValidationError(
`DocType ${this.name}: "fieldname" is required for field at index ${i}`
);
}
if (!df.fieldtype) {
throw new frappe.errors.ValidationError(
`DocType ${this.name}: "fieldtype" is required for field "${df.fieldname}"`
);
}
});
const doctypeFields = this.fields.map(field => field.fieldname);
// standard fields // standard fields
for (let field of model.commonFields) { for (let field of model.commonFields) {
if ( if (
frappe.db.typeMap[field.fieldtype] && frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname) !doctypeFields.includes(field.fieldname)
) { ) {
_add(field); _add(field);
} }
@ -178,7 +192,7 @@ module.exports = class BaseMeta extends BaseDocument {
for (let field of model.childFields) { for (let field of model.childFields) {
if ( if (
frappe.db.typeMap[field.fieldtype] && frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname) !doctypeFields.includes(field.fieldname)
) { ) {
_add(field); _add(field);
} }
@ -188,7 +202,7 @@ module.exports = class BaseMeta extends BaseDocument {
for (let field of model.parentFields) { for (let field of model.parentFields) {
if ( if (
frappe.db.typeMap[field.fieldtype] && frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname) !doctypeFields.includes(field.fieldname)
) { ) {
_add(field); _add(field);
} }
@ -200,7 +214,7 @@ module.exports = class BaseMeta extends BaseDocument {
for (let field of model.treeFields) { for (let field of model.treeFields) {
if ( if (
frappe.db.typeMap[field.fieldtype] && frappe.db.typeMap[field.fieldtype] &&
!doctype_fields.includes(field.fieldname) !doctypeFields.includes(field.fieldname)
) { ) {
_add(field); _add(field);
} }
@ -270,7 +284,8 @@ module.exports = class BaseMeta extends BaseDocument {
} }
if (!validValues.includes(value)) { if (!validValues.includes(value)) {
throw new frappe.errors.ValueError( throw new frappe.errors.ValueError(
`Invalid value "${value}" for "${field.label}". Must be one of ${options.join(', ')}` // prettier-ignore
`DocType ${this.name}: Invalid value "${value}" for "${field.label}". Must be one of ${options.join(', ')}`
); );
} }
return value; return value;