From d582c67c629d001d4491d7ba9dd376ad2147a08d Mon Sep 17 00:00:00 2001 From: John Cardinal Date: Wed, 16 Jan 2019 19:14:50 +0000 Subject: [PATCH] --- .../docs/api-validation-error-codes.md | 3 +- server/AyaNova/biz/CustomFieldsValidator.cs | 4 +- server/AyaNova/biz/FormAvailableFields.cs | 93 +++++++++++-------- server/AyaNova/biz/RequiredFieldsValidator.cs | 45 +++++++++ server/AyaNova/biz/ValidationErrorType.cs | 3 +- server/AyaNova/biz/WidgetBiz.cs | 1 + 6 files changed, 104 insertions(+), 45 deletions(-) create mode 100644 server/AyaNova/biz/RequiredFieldsValidator.cs 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 47256e60..36aabd9e 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 value is empty | +| RequiredPropertyEmpty | An AyaNova fixed rule required property value is empty | +| CustomRequiredPropertyEmpty | A user customized form property set to required has an empty value | | 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 | diff --git a/server/AyaNova/biz/CustomFieldsValidator.cs b/server/AyaNova/biz/CustomFieldsValidator.cs index 433e78f7..6018d1f8 100644 --- a/server/AyaNova/biz/CustomFieldsValidator.cs +++ b/server/AyaNova/biz/CustomFieldsValidator.cs @@ -32,7 +32,7 @@ namespace AyaNova.Biz if (ThisFormCustomFieldsList.Contains(fldKey) && fldRequired == true) { //Ok, this field is required but custom fields are all empty so add this error - biz.AddError(ValidationErrorType.RequiredPropertyEmpty, fldKey); + biz.AddError(ValidationErrorType.CustomRequiredPropertyEmpty, fldKey); } } @@ -61,7 +61,7 @@ namespace AyaNova.Biz var fldRequired = jo["required"].Value(); if (fldRequired && string.IsNullOrWhiteSpace(CurrentValue)) { - biz.AddError(ValidationErrorType.RequiredPropertyEmpty, iFldKey); + biz.AddError(ValidationErrorType.CustomRequiredPropertyEmpty, iFldKey); } break; } diff --git a/server/AyaNova/biz/FormAvailableFields.cs b/server/AyaNova/biz/FormAvailableFields.cs index 294cafad..25501ac4 100644 --- a/server/AyaNova/biz/FormAvailableFields.cs +++ b/server/AyaNova/biz/FormAvailableFields.cs @@ -31,17 +31,17 @@ namespace AyaNova.Biz List l = new List(); switch (key) { - case WIDGET_FORM_KEY: - l.Add(new FormField("WidgetName", false, false));//is not shared localized text key and not hideable as it is in the validation rules for widget - l.Add(new FormField("WidgetSerial"));//not in validation rules...you get the idea - l.Add(new FormField("WidgetDollarAmount")); - l.Add(new FormField("WidgetCount")); - l.Add(new FormField("WidgetRoles")); - l.Add(new FormField("WidgetStartDate", false, false)); - l.Add(new FormField("WidgetEndDate", false, false)); - l.Add(new FormField("WidgetNotes")); - l.Add(new FormField("CommonActive", true)); - l.Add(new FormField("Tags", true)); + case WIDGET_FORM_KEY: + l.Add(new FormField("WidgetName", "Name", false, false));//is not shared localized text key and not hideable as it is in the validation rules for widget + l.Add(new FormField("WidgetSerial", "Serial"));//not in validation rules...you get the idea + l.Add(new FormField("WidgetDollarAmount", "DollarAmount")); + l.Add(new FormField("WidgetCount", "Count")); + l.Add(new FormField("WidgetRoles", "Roles")); + l.Add(new FormField("WidgetStartDate", "StartDate", false, false)); + l.Add(new FormField("WidgetEndDate", "EndDate", false, false)); + l.Add(new FormField("WidgetNotes", "Notes")); + l.Add(new FormField("CommonActive", "Active", true)); + l.Add(new FormField("Tags", "Tags", true)); l.Add(new FormField("WidgetCustom1", false, true, true)); l.Add(new FormField("WidgetCustom2", false, true, true)); l.Add(new FormField("WidgetCustom3", false, true, true)); @@ -60,37 +60,37 @@ namespace AyaNova.Biz l.Add(new FormField("WidgetCustom16", false, true, true)); break; - // case USER_FORM_KEY: - // l.Add(new FormField("WidgetName", false, false));//is not shared localized text key and not hideable as it is in the validation rules for widget - // l.Add(new FormField("WidgetSerial"));//not in validation rules...you get the idea - // l.Add(new FormField("WidgetDollarAmount")); - // l.Add(new FormField("WidgetCount")); - // l.Add(new FormField("WidgetRoles")); - // l.Add(new FormField("WidgetStartDate", false, false)); - // l.Add(new FormField("WidgetEndDate", false, false)); - // l.Add(new FormField("WidgetNotes")); - // l.Add(new FormField("CommonActive", true)); - // l.Add(new FormField("Tags", true)); - // l.Add(new FormField("WidgetCustom1", false, true, true)); - // l.Add(new FormField("WidgetCustom2", false, true, true)); - // l.Add(new FormField("WidgetCustom3", false, true, true)); - // l.Add(new FormField("WidgetCustom4", false, true, true)); - // l.Add(new FormField("WidgetCustom5", false, true, true)); - // l.Add(new FormField("WidgetCustom6", false, true, true)); - // l.Add(new FormField("WidgetCustom7", false, true, true)); - // l.Add(new FormField("WidgetCustom8", false, true, true)); - // l.Add(new FormField("WidgetCustom9", false, true, true)); - // l.Add(new FormField("WidgetCustom10", false, true, true)); - // l.Add(new FormField("WidgetCustom11", false, true, true)); - // l.Add(new FormField("WidgetCustom12", false, true, true)); - // l.Add(new FormField("WidgetCustom13", false, true, true)); - // l.Add(new FormField("WidgetCustom14", false, true, true)); - // l.Add(new FormField("WidgetCustom15", false, true, true)); - // l.Add(new FormField("WidgetCustom16", false, true, true)); - // break; + // case USER_FORM_KEY: + // l.Add(new FormField("WidgetName", false, false));//is not shared localized text key and not hideable as it is in the validation rules for widget + // l.Add(new FormField("WidgetSerial"));//not in validation rules...you get the idea + // l.Add(new FormField("WidgetDollarAmount")); + // l.Add(new FormField("WidgetCount")); + // l.Add(new FormField("WidgetRoles")); + // l.Add(new FormField("WidgetStartDate", false, false)); + // l.Add(new FormField("WidgetEndDate", false, false)); + // l.Add(new FormField("WidgetNotes")); + // l.Add(new FormField("CommonActive", true)); + // l.Add(new FormField("Tags", true)); + // l.Add(new FormField("WidgetCustom1", false, true, true)); + // l.Add(new FormField("WidgetCustom2", false, true, true)); + // l.Add(new FormField("WidgetCustom3", false, true, true)); + // l.Add(new FormField("WidgetCustom4", false, true, true)); + // l.Add(new FormField("WidgetCustom5", false, true, true)); + // l.Add(new FormField("WidgetCustom6", false, true, true)); + // l.Add(new FormField("WidgetCustom7", false, true, true)); + // l.Add(new FormField("WidgetCustom8", false, true, true)); + // l.Add(new FormField("WidgetCustom9", false, true, true)); + // l.Add(new FormField("WidgetCustom10", false, true, true)); + // l.Add(new FormField("WidgetCustom11", false, true, true)); + // l.Add(new FormField("WidgetCustom12", false, true, true)); + // l.Add(new FormField("WidgetCustom13", false, true, true)); + // l.Add(new FormField("WidgetCustom14", false, true, true)); + // l.Add(new FormField("WidgetCustom15", false, true, true)); + // l.Add(new FormField("WidgetCustom16", false, true, true)); + // break; + - default: throw new System.ArgumentOutOfRangeException($"FormAvailableFields: {key} is not a valid form key"); } @@ -104,15 +104,26 @@ namespace AyaNova.Biz public class FormField { public string Key { get; set; } + public string PropertyName { get; set; } public bool Hideable { get; set; } public bool SharedLTKey { get; set; } public bool Custom { get; set; } - public FormField(string key, bool sharedLTKey = false, bool hideable = true, bool custom = false) + public FormField(string key, string propertyName, bool sharedLTKey = false, bool hideable = true, bool custom = false) { Key = key; Hideable = hideable; Custom = custom; SharedLTKey = sharedLTKey; + PropertyName = propertyName; + } + + public FormField(string key, bool sharedLTKey = false, bool hideable = true, bool custom = false) + { + Key = key; + Hideable = hideable; + Custom = custom; + SharedLTKey = sharedLTKey; + PropertyName = null; } } diff --git a/server/AyaNova/biz/RequiredFieldsValidator.cs b/server/AyaNova/biz/RequiredFieldsValidator.cs new file mode 100644 index 00000000..d84d3eef --- /dev/null +++ b/server/AyaNova/biz/RequiredFieldsValidator.cs @@ -0,0 +1,45 @@ +using AyaNova.Models; +using System.Linq; +using Newtonsoft.Json.Linq; + + +namespace AyaNova.Biz +{ + //VALIDATE REQUIRED FIELDS THAT ARE NOT CUSTOM + + internal static class RequiredFieldsValidator + { + internal static void Validate(BizObject biz, FormCustom formCustom, object proposedObject) + { + //No form custom = no template to check against so nothing to do + if (formCustom == null) + return; + + var FormTemplate = JArray.Parse(formCustom.Template); + var FormFields = FormAvailableFields.FormFields(formCustom.FormKey); + // var ThisFormNormalFieldsList = FormFields.Where(x => x.Custom == false).Select(x => x.Key).ToList(); + + foreach (JObject jo in FormTemplate) + { + if (jo["required"].Value() == true) + { + //get the actual property name + //First get the LT key + var FldLtKey = jo["fld"].Value(); + + // - e.g.: {template:[{fld:"ltkeyfieldname",hide:"true/false",required:"true/false", type:"bool"},{fld:"ltkeyfieldname",hide:"true/false",required:"true/false", type:"text"]} + + + string RequiredPropertyName = FormFields.Where(x => x.Key == FldLtKey).Single().PropertyName; + + //use reflection to get the underlying value + object propertyValue = proposedObject.GetType().GetProperty(RequiredPropertyName).GetValue(proposedObject, null); + if (propertyValue == null || string.IsNullOrWhiteSpace(propertyValue.ToString())) + biz.AddError(ValidationErrorType.RequiredPropertyEmpty, FldLtKey); + break; + } + } + } + + }//eoc +}//ens diff --git a/server/AyaNova/biz/ValidationErrorType.cs b/server/AyaNova/biz/ValidationErrorType.cs index 01108622..8219ce01 100644 --- a/server/AyaNova/biz/ValidationErrorType.cs +++ b/server/AyaNova/biz/ValidationErrorType.cs @@ -11,7 +11,8 @@ namespace AyaNova.Biz ReferentialIntegrity = 6, InvalidOperation = 7, NotChangeable=8, - RequiredPropertyMissing = 9 + RequiredPropertyMissing = 9, + CustomRequiredPropertyEmpty = 10 //!! NOTE - UPDATE api-validation-error-codes.md documentation when adding items diff --git a/server/AyaNova/biz/WidgetBiz.cs b/server/AyaNova/biz/WidgetBiz.cs index 52f29d1c..7f3844a0 100644 --- a/server/AyaNova/biz/WidgetBiz.cs +++ b/server/AyaNova/biz/WidgetBiz.cs @@ -406,6 +406,7 @@ namespace AyaNova.Biz //Yeppers, do the validation, there are two, the custom fields and the regular fields that might be set to required //validate users choices for required non custom fields + RequiredFieldsValidator.Validate(this,FormCustomization, proposedObj); //validate custom fields CustomFieldsValidator.Validate(this, FormCustomization, proposedObj.CustomFields);