Пример #1
0
        public IEnumerable <ValidationResult> Validate(object value, DocumentValidationContext <T> validationContext)
        {
            if (Unless != null && Unless(validationContext.Document))
            {
                yield break;
            }

            if (On != SaveType.Any && On != validationContext.SaveType)
            {
                yield break;
            }

            // TODO: Move this to child validators.
            if (value == null && !AllowNull)
            {
                yield return(new ValidationResult(validationContext.DisplayName + " is required"));

                yield break;
            }

            foreach (var validationResult in ValidateInternal(value, validationContext))
            {
                yield return(validationResult);
            }
        }
Пример #2
0
 protected override IEnumerable <ValidationResult> ValidateInternal(object value,
                                                                    DocumentValidationContext <T> validationContext)
 {
     if (value == null || (value is string && string.IsNullOrEmpty(((string)value).Trim())))
     {
         yield return(new ValidationResult(validationContext.DisplayName + " is required"));
     }
 }
Пример #3
0
        protected override sealed IEnumerable <ValidationResult> ValidateInternal(object value,
                                                                                  DocumentValidationContext <T> validationContext)
        {
            var result = new List <ValidationResult>();

            foreach (var validator in _validators)
            {
                result.AddRange(validator.Validate(value, validationContext));
            }
            return(result);
        }
Пример #4
0
        public static IEnumerable <ValidationResult> Validate <T>(
            Dictionary <Func <T, object>, IValidationBuilder <T> > validators,
            DocumentValidationContext <T> validationContext)
        {
            var results = new List <ValidationResult>();

            foreach (var kvp in validators)
            {
                var value = kvp.Key(validationContext.Document);
                foreach (var validator in kvp.Value.Validators)
                {
                    results.AddRange(validator.Validate(value, validationContext));
                }
            }
            return(results);
        }
Пример #5
0
        protected override IEnumerable <ValidationResult> ValidateInternal(object value,
                                                                           DocumentValidationContext <T> validationContext)
        {
            var typedValue = value as string;

            if (string.IsNullOrEmpty(typedValue))
            {
                yield break;
            }

            if (typedValue.Length < _minimumLength)
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be at least " + _minimumLength + " characters long"));
            }
            if (typedValue.Length > _maximumLength)
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be at most " + _maximumLength + " characters long"));
            }
        }
Пример #6
0
        protected override IEnumerable <ValidationResult> ValidateInternal(object value,
                                                                           DocumentValidationContext <T> validationContext)
        {
            var typedValue = (float)Convert.ChangeType(value, typeof(float));

            if (GreaterThanOrEqualTo != null && !(typedValue >= GreaterThanOrEqualTo.Value))
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be greater than or equal to " + GreaterThanOrEqualTo.Value));
            }
            if (GreaterThan != null && !(typedValue > GreaterThan.Value))
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be greater than " + GreaterThan.Value));
            }

            if (LessThanOrEqualTo != null && !(typedValue >= LessThanOrEqualTo.Value))
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be Less than or equal to " + LessThanOrEqualTo.Value));
            }
            if (LessThan != null && !(typedValue > LessThan.Value))
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be Less than " + LessThan.Value));
            }
        }
Пример #7
0
 protected abstract IEnumerable <ValidationResult> ValidateInternal(object value,
                                                                    DocumentValidationContext <T> validationContext);
Пример #8
0
        protected override IEnumerable <ValidationResult> ValidateInternal(object value, DocumentValidationContext <T> validationContext)
        {
            var typedValue = value as string;

            if (string.IsNullOrEmpty(typedValue))
            {
                yield break;
            }

            if (!Regex.IsMatch(typedValue, _regex))
            {
                yield return(new ValidationResult(validationContext.DisplayName + " does not match format"));
            }
        }
Пример #9
0
 protected override IEnumerable <ValidationResult> ValidateInternal(object value, DocumentValidationContext <T> validationContext)
 {
     if (!_items.Contains((TProperty)value))
     {
         yield return(new ValidationResult(validationContext.DisplayName + " is not valid"));
     }
 }
        protected override IEnumerable <ValidationResult> ValidateInternal(object value, DocumentValidationContext <TDocument> validationContext)
        {
            var typedValue = value as string;

            if (string.IsNullOrEmpty(typedValue))
            {
                yield break;
            }

            if (!CaseSensitive)
            {
                typedValue = typedValue.ToLower();
            }

            var andClauses = new List <IMongoQuery>();

            andClauses.Add(Query.NE("_id", validationContext.Document.ID));

            andClauses.Add(CaseSensitive
                                ? Query.EQ(_propertyName, BsonValue.Create(typedValue))
                                : Query.Matches(_propertyName, new BsonRegularExpression("^" + Regex.Escape(typedValue) + "$", "i")));

            if (Scope != null && Scope.Length > 0)
            {
                foreach (var scopeProperty in Scope)
                {
                    string scopePropertyName  = ExpressionUtility.GetPropertyName(scopeProperty);
                    object scopePropertyValue = scopeProperty.Compile()(validationContext.Document);
                    if (scopePropertyValue != null &&
                        ReflectionUtility.IsSubclassOfRawGeneric(typeof(Document <>), scopePropertyValue.GetType()))
                    {
                        scopePropertyValue = DocumentUtility.GetDocumentID(scopePropertyValue);
                    }
                    andClauses.Add(Query.EQ(scopePropertyName, BsonValue.Create(scopePropertyValue)));
                }
            }

            var query   = Query.And(andClauses.ToArray());
            var results = Document <TDocument> .GetCollection().Find(query);

            if (results.Any())
            {
                yield return(new ValidationResult(validationContext.DisplayName + " must be unique"));
            }
        }
        protected override IEnumerable <ValidationResult> ValidateInternal(object value, DocumentValidationContext <T> validationContext)
        {
            // Get all documents embeddded in the same collection in the parent document.
            var parentCollection = EmbeddedDocumentUtility.GetEmbeddedCollection(
                validationContext.ParentDocument,
                validationContext.ParentPropertyName);

            // Loop through those embedded documents, excluding this one.
            foreach (T item in parentCollection.Where(x => x != validationContext.Document))
            {
                // Compare the specified property against this value.
                if (Equals(_propertyExpression(item), (TProperty)value))
                {
                    yield return(new ValidationResult(validationContext.DisplayName + " must be unique"));

                    yield break;
                }
            }
        }