コード例 #1
0
        public IValidationResult Validate(T?obj, ISet <string> fields)
        {
            if (obj == null)
            {
                return(ModelValidationResult.Failure(Name, ValidationType.ONLY_EXISTS, nameof(T), "", "Parent object is not set"));
            }

            var fieldExistenceMap = GetFields(obj);


            var setFields = fieldExistenceMap.Where(v => v.Value && fields.Contains(v.Key)).Select(v => v.Key);

            if (setFields.Count() != fields.Count())
            {
                return(ModelValidationResult.Failure(Name, ValidationType.ONLY_EXISTS, nameof(T), "",
                                                     $"All fields are not set.  Expected [{string.Join(',', fields)}] but found only [{string.Join(',', setFields)}]."));
            }

            var otherSetFields = fieldExistenceMap.Where(v => v.Value && !fields.Contains(v.Key)).Select(v => v.Key);

            if (otherSetFields.Any())
            {
                return(ModelValidationResult.Failure(Name, ValidationType.ONLY_EXISTS, nameof(T), "",
                                                     $"[{string.Join(',', fields)}] are not the only fields set. Other set fields: {string.Join(',', otherSetFields)}"));
            }
            return(ModelValidationResult.Success(Name, ValidationType.ONLY_EXISTS, nameof(T), ""));
        }
コード例 #2
0
        public IValidationResult Validate(T obj)
        {
            var validationMethod    = ValidationMethod;
            var populatedFieldNames = GetPopulatedFieldNames(obj);

            if (validationMethod.Check(populatedFieldNames.Count))
            {
                return(ModelValidationResult.Success(Name, ValidationType.CHOICE_RULE, nameof(T)));
            }
            return(new ChoiceRuleFailure(Name, nameof(T), ChoiceFieldNames, populatedFieldNames, validationMethod));
        }
コード例 #3
0
        public IValidationResult Validate(T?obj)
        {
            if (obj == null)
            {
                return(ModelValidationResult.Failure(Name, ValidationType.MODEL_INSTANCE, nameof(T), "", "Object is not set"));
            }

            var errors = GetResults(obj).Where(r => !r.Result);

            if (errors.Any())
            {
                return(ModelValidationResult.Failure(Name, ValidationType.MODEL_INSTANCE, nameof(T), "", string.Join("; ", errors.Select(r => r.Error))));
            }

            return(ModelValidationResult.Success(Name, ValidationType.MODEL_INSTANCE, nameof(T), ""));
        }
コード例 #4
0
        public IValidationResult Validate(T obj)
        {
            IComparisonResult?result = ExecuteDataRule(obj);

            if (result == null)
            {
                // Rule is not applicable
                return(ModelValidationResult.Success(Name, ValidationType.DATA_RULE, nameof(T)));
            }
            if (result.Result)
            {
                // Rule passed
                return(ModelValidationResult.Success(Name, ValidationType.DATA_RULE, nameof(T), Definition));
            }

            // Rule failed
            return(ModelValidationResult.Failure(Name, ValidationType.DATA_RULE, nameof(T), Definition, result.Error));
        }
コード例 #5
0
        public IValidationResult Validate(T?obj, string field)
        {
            if (obj == null)
            {
                return(ModelValidationResult.Failure(Name, ValidationType.ONLY_EXISTS, nameof(T), "", "Parent object is not set"));
            }

            var fieldExistenceMap = GetFields(obj);

            if (!fieldExistenceMap[field])
            {
                return(ModelValidationResult.Failure(Name, ValidationType.ONLY_EXISTS, nameof(T), "",
                                                     $"[{field}] is not set."));
            }

            var fields = fieldExistenceMap.Where(v => v.Value && !v.Key.Equals(field)).Select(v => v.Key);

            if (fields.Any())
            {
                return(ModelValidationResult.Failure(Name, ValidationType.ONLY_EXISTS, nameof(T), "",
                                                     $"[{field}] is not the only field set. Other set fields: {string.Join(',', fields)}"));
            }
            return(ModelValidationResult.Success(Name, ValidationType.ONLY_EXISTS, nameof(T), ""));
        }