From 595e3c12843861932b90a47492d25f88601f601c Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Tue, 15 Jan 2019 23:14:02 +0000 Subject: [PATCH] --- devdocs/todo.txt | 5 ++- .../docs/api-validation-error-codes.md | 3 +- server/AyaNova/biz/CustomFieldsValidator.cs | 41 ++++++++++++++++--- server/AyaNova/biz/ValidationErrorType.cs | 3 +- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/devdocs/todo.txt b/devdocs/todo.txt index 29245fd6..28ad66eb 100644 --- a/devdocs/todo.txt +++ b/devdocs/todo.txt @@ -12,8 +12,9 @@ Do the stuff in the Client todo first then back to the server as required. ----------------------- SERVER - - FormCustomBiz Validation code needs to be written - - And test to test validation + + - Add validation of custom fields in widget + - Add validation of required fields that are not custom in widget - Then do the things all the way down to JWT issues below diff --git a/docs/8.0/ayanova/docs/api-validation-error-codes.md b/docs/8.0/ayanova/docs/api-validation-error-codes.md index ba05ed4c..47256e60 100644 --- a/docs/8.0/ayanova/docs/api-validation-error-codes.md +++ b/docs/8.0/ayanova/docs/api-validation-error-codes.md @@ -5,7 +5,8 @@ In each case there may be more details in the `message` property where appropria | CODE | MEANING | | ----- | ------------------------------ | -| RequiredPropertyEmpty | Required property is missing or empty | +| RequiredPropertyEmpty | Required property value is empty | +| RequiredPropertyMissing | Required property is missing entirely | | LengthExceeded | A text property has more characters than are allowed. The limit will be returned in the `message` property of the validation error | | NotUnique | A text property is required to be unique but an existing identical value was found in the database | | StartDateMustComeBeforeEndDate | When an object requires a start and end date the start date must be earlier than the end date | diff --git a/server/AyaNova/biz/CustomFieldsValidator.cs b/server/AyaNova/biz/CustomFieldsValidator.cs index 314040ed..433e78f7 100644 --- a/server/AyaNova/biz/CustomFieldsValidator.cs +++ b/server/AyaNova/biz/CustomFieldsValidator.cs @@ -17,7 +17,6 @@ namespace AyaNova.Biz var FormTemplate = JArray.Parse(formCustom.Template); - var ThisFormCustomFieldsList = FormAvailableFields.FormFields(formCustom.FormKey).Where(x => x.Custom == true).Select(x => x.Key).ToList(); //If the customFields string is empty then only validation is if any of the fields are required to be filled in @@ -27,8 +26,8 @@ namespace AyaNova.Biz for (int i = 0; i < FormTemplate.Count; i++) { //get the field customization - var fldKey = FormTemplate["fld"].Value(); - var fldRequired = FormTemplate["required"].Value(); + var fldKey = FormTemplate[i]["fld"].Value(); + var fldRequired = FormTemplate[i]["required"].Value(); //Check if this is an expected custom field and that it was set to required if (ThisFormCustomFieldsList.Contains(fldKey) && fldRequired == true) { @@ -41,10 +40,42 @@ namespace AyaNova.Biz } //here we have both a bunch of custom fields presumeably and a form customization so let's get cracking... + //parse the custom fields, it should contain an object with 16 keys var CustomFieldData = JObject.Parse(customFields); + //make sure all the keys are present + foreach (string iFldKey in ThisFormCustomFieldsList) + { + if (CustomFieldData.ContainsKey(iFldKey)) + { + //validate for now that the custom fields set as required have data in them. Note that we are not validating the sanity of the values, only that they exist + //trying to build in slack for when users inevitably change the TYPE of the custom field + //Maybe in future this will be handled more thoroughly here but for now just make sure it's been filled in + + //validate it + string CurrentValue = CustomFieldData[iFldKey].Value(); + foreach (JObject jo in FormTemplate) + { + if (jo["fld"].Value() == iFldKey) + { + var fldRequired = jo["required"].Value(); + if (fldRequired && string.IsNullOrWhiteSpace(CurrentValue)) + { + biz.AddError(ValidationErrorType.RequiredPropertyEmpty, iFldKey); + } + break; + } + } + + } + else + { + //This is a serious issue and invalidates all + biz.AddError(ValidationErrorType.RequiredPropertyMissing, iFldKey); + } + } } - } -} + }//eoc +}//ens diff --git a/server/AyaNova/biz/ValidationErrorType.cs b/server/AyaNova/biz/ValidationErrorType.cs index 26d8b5f4..01108622 100644 --- a/server/AyaNova/biz/ValidationErrorType.cs +++ b/server/AyaNova/biz/ValidationErrorType.cs @@ -10,7 +10,8 @@ namespace AyaNova.Biz InvalidValue = 5, ReferentialIntegrity = 6, InvalidOperation = 7, - NotChangeable=8 + NotChangeable=8, + RequiredPropertyMissing = 9 //!! NOTE - UPDATE api-validation-error-codes.md documentation when adding items