Skip to content

Commit

Permalink
Fix the validation errors code. See #67639
Browse files Browse the repository at this point in the history
  • Loading branch information
novaxpro committed Oct 12, 2023
1 parent f870c28 commit b36edba
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 41 deletions.
Binary file added server/.DS_Store
Binary file not shown.
97 changes: 56 additions & 41 deletions server/validation_errors/validation_errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Validation Errors dynamic example.
This is a dynamic validation error example, it will generate a validation error
for each field for the objecttype "validation_errors" (this is configured in the manifest.master.yml)
This is a dynamic validation error example, it will generate a validation error
for each field for the objecttype "validation_errors" if the field is not empty.
With this example you can easily test the validation error handling in the editor for all fields.
The general rules for the field names are:
Expand All @@ -24,23 +24,23 @@ An example of a response with validation errors generated by this script is:
[
{
"field": "myobjecttype.field_list[0].subfield_1",
"message": "This is a validation error for field 'subfield_1' in the first position of the field_list nested",
"message": "message of the error",
"parameters": {
"a": "a",
"b": "b"
}
},
{
"field": "myobjecttype.field_list[]",
"message": "This is a validation error for the field_list nested itself",
"message": "message of the error",
"parameters": {
"a": "a",
"b": "b"
}
},
{
"field": "myobjecttype.name",
"message": "This is a validation error for the name field",
"message": "message of the error",
"parameters": {
"a": "a",
"b": "b"
Expand All @@ -49,7 +49,7 @@ An example of a response with validation errors generated by this script is:
{
// Note that for reverse nested fields we use first the reverse objecttype name and then the reverse field name
"field": "otherObjectType.reverse_field[0].name",
"message": "This is a validation error for the field name in the first reverse_field of type otherObjectType",
"message": "message of the error",
"parameters": {
"a": "a",
"b": "b"
Expand Down Expand Up @@ -101,73 +101,88 @@ process.stdin.on('end', () => {
continue;
}

// Demo nested errors only for even indexes
// Demo nested errors
if (fieldName.startsWith("_nested") && fieldValue.length > 0) {
let nestedProblemsCount = 0;
let idx = 0;
fieldValue.forEach(nestedObject => {
if (idx % 2 === 0) {
for (const nestedFieldName in nestedObject) {
const nestedFieldValue = nestedObject[nestedFieldName];
if (nestedFieldValue === null) {
continue;
}

const n = nestedFieldName.startsWith("_nested") ? nestedFieldName.substring(fieldName.length + 2) + "[]" : nestedFieldName;
const f = fieldName.substring(10 + objecttype.length);

e.push({
"field": objecttype + "." + f + "[" + idx + "]." + n,
"message": `This is a dummy error message for ${nestedFieldName} with value ${nestedFieldValue}, we only accept empty values here...`,
"parameters": {
a: "a",
b: "b",
}
});
problemsCount++;
for (const nestedFieldName in nestedObject) {
const nestedFieldValue = nestedObject[nestedFieldName];
if (nestedFieldValue === null) {
continue;
}

const n = nestedFieldName.startsWith("_nested") ? nestedFieldName.substring(fieldName.length + 2) + "[]" : nestedFieldName;
const f = fieldName.substring(10 + objecttype.length);

e.push({
"field": objecttype + "." + f + "[" + idx + "]." + n,
"message": `Error on **${nestedFieldName}** with value, fields can only be empty`,
"parameters": {
a: "a",
b: "b",
}
});
nestedProblemsCount++;
problemsCount++;
}
idx++;
});

// For a validation error for the nested table itself we can use <subtable_name>[] as field name
const newNestedFieldName = fieldName.substring(10 + objecttype.length) + "[]";
e.push({
"field": objecttype + "." + newNestedFieldName,
"message": `This is a dummy error message for ${newNestedFieldName} with value ${fieldValue}, we only accept empty values here...`,
"parameters": {
a: "a",
b: "b",
}
});
if(nestedProblemsCount>0) {
const newNestedFieldName = fieldName.substring(10 + objecttype.length) + "[]";
e.push({
"field": objecttype + "." + newNestedFieldName,
"message": `There are errors inside **${newNestedFieldName}**`,
"parameters": {
a: "a",
b: "b",
}
});
};

} else if (fieldName.startsWith("_reverse_nested") && fieldValue.length > 0) {
// Reverse nested fields, the reverse nested is like _reverse_nested_<linked_table>:<field_name>
const reverseTableName = fieldName.split(":")[1];
const reverseFieldName = fieldName.split(":")[2];
e.push({
"field": reverseTableName + "." + reverseFieldName + "[]",
"message": `This is a dummy error message for the reverse nested field ${fieldName}`,
});
let reverseProblemsCount = 0;
let idx = 0;
fieldValue.forEach(nestedReversedObject => {
// We iterate the fields of the reverse nested object
for (const nestedFieldName in nestedReversedObject) {

if (nestedFieldName.startsWith("_")) {
continue;
}
if(nestedReversedObject[nestedFieldName] === null) {
continue
}
e.push({
"field": reverseTableName + "." + reverseFieldName + "[" + idx + "]." + nestedFieldName,
"message": `This is a dummy error message for the reverse nested field ${nestedFieldName} in ${reverseFieldName}`,
"message": `Error for **${nestedFieldName}** in **${reverseFieldName}**, fields can only be empty`,
});
reverseProblemsCount++;
}
idx++;
});

if(reverseProblemsCount>0) {
e.push({
"field": reverseTableName + "." + reverseFieldName + "[]",
"message": `There is a problem on the field: **${fieldName}**`,
});
}

} else {
if (Array.isArray(fieldValue) && fieldValue.length === 0) {
continue;
}

// Not nested fields
e.push({
"field": objecttype + "." + fieldName,
"message": `This is a dummy error message for ${fieldName} with value ${fieldValue}, we only accept empty values here...`,
"message": `The field **${fieldName}** only accepts empty values`,
"parameters": {
a: "a",
b: "b",
Expand Down

0 comments on commit b36edba

Please sign in to comment.