예제 #1
0
 private void ValidateIsImage(IAssetInfo asset, ImmutableQueue <string> path, AddError addError)
 {
     if (properties.MustBeImage && asset.Type != AssetType.Image && asset.MimeType != "image/svg+xml")
     {
         addError(path, T.Get("contents.validation.image"));
     }
 }
예제 #2
0
        public async Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            var count = context.Path.Count();

            if (value != null && (count == 0 || (count == 2 && context.Path.Last() == InvariantPartitioning.Instance.Master.Key)))
            {
                FilterNode filter = null;

                if (value is string s)
                {
                    filter = new FilterComparison(Path(context), FilterOperator.Equals, new FilterValue(s));
                }
                else if (value is double d)
                {
                    filter = new FilterComparison(Path(context), FilterOperator.Equals, new FilterValue(d));
                }

                if (filter != null)
                {
                    var found = await context.GetContentIdsAsync(context.SchemaId, filter);

                    if (found.Any(x => x != context.ContentId))
                    {
                        addError(context.Path, "Another content with the same value exists.");
                    }
                }
            }
        }
예제 #3
0
        public Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value is string stringValue)
            {
                if (!string.IsNullOrEmpty(stringValue))
                {
                    try
                    {
                        if (!regex.IsMatch(stringValue))
                        {
                            if (string.IsNullOrWhiteSpace(errorMessage))
                            {
                                addError(context.Path, "Does not match to the pattern.");
                            }
                            else
                            {
                                addError(context.Path, errorMessage);
                            }
                        }
                    }
                    catch
                    {
                        addError(context.Path, "Regex is too slow.");
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #4
0
 private void ValidateNonImage(ImmutableQueue <string> path, AddError addError)
 {
     if (properties.MustBeImage)
     {
         addError(path, T.Get("contents.validation.image"));
     }
 }
예제 #5
0
        public async Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (context.Mode == ValidationMode.Optimized)
            {
                return;
            }

            var count = context.Path.Count();

            if (value != null && (count == 0 || (count == 2 && context.Path.Last() == InvariantPartitioning.Key)))
            {
                FilterNode <ClrValue>?filter = null;

                if (value is string s)
                {
                    filter = ClrFilter.Eq(Path(context), s);
                }
                else if (value is double d)
                {
                    filter = ClrFilter.Eq(Path(context), d);
                }

                if (filter != null)
                {
                    var found = await checkUniqueness(filter);

                    if (found.Any(x => x.Id != context.ContentId))
                    {
                        addError(context.Path, "Another content with the same value exists.");
                    }
                }
            }
        }
        public Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value is string stringValue && !string.IsNullOrEmpty(stringValue))
            {
                if (minLength.HasValue && maxLength.HasValue)
                {
                    if (minLength == maxLength && minLength != stringValue.Length)
                    {
                        addError(context.Path, $"Must have exactly {maxLength} character(s).");
                    }
                    else if (stringValue.Length < minLength || stringValue.Length > maxLength)
                    {
                        addError(context.Path, $"Must have between {minLength} and {maxLength} character(s).");
                    }
                }
                else
                {
                    if (minLength.HasValue && stringValue.Length < minLength.Value)
                    {
                        addError(context.Path, $"Must have at least {minLength} character(s).");
                    }

                    if (maxLength.HasValue && stringValue.Length > maxLength.Value)
                    {
                        addError(context.Path, $"Must not have more than {maxLength} character(s).");
                    }
                }
            }

            return(TaskHelper.Done);
        }
예제 #7
0
        public async Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            var count = context.Path.Count();

            if (value != null && (count == 0 || (count == 2 && context.Path.Last() == InvariantPartitioning.Key)))
            {
                FilterNode <ClrValue>?filter = null;

                if (value is string s)
                {
                    filter = ClrFilter.Eq(Path(context), s);
                }
                else if (value is double d)
                {
                    filter = ClrFilter.Eq(Path(context), d);
                }

                if (filter != null)
                {
                    var found = await checkUniqueness(filter);

                    if (found.Any(x => x.Id != context.ContentId))
                    {
                        addError(context.Path, T.Get("contents.validation.unique"));
                    }
                }
            }
        }
예제 #8
0
        public async Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (context.Mode == ValidationMode.Optimized)
            {
                return;
            }

            if (value is ICollection <DomainId> contentIds)
            {
                var foundIds = await checkReferences(contentIds.ToHashSet());

                foreach (var id in contentIds)
                {
                    var(schemaId, _) = foundIds.FirstOrDefault(x => x.Id == id);

                    if (schemaId == DomainId.Empty)
                    {
                        addError(context.Path, T.Get("common.referenceNotFound", new { id }));
                    }
                    else if (schemaIds?.Any() == true && !schemaIds.Contains(schemaId))
                    {
                        addError(context.Path, T.Get("common.referenceToInvalidSchema", new { id }));
                    }
                }
            }
        }
예제 #9
0
        public async Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            try
            {
                var typedValue = value;

                if (value is IJsonValue jsonValue)
                {
                    if (jsonValue.Type == JsonValueType.Null)
                    {
                        typedValue = null;
                    }
                    else
                    {
                        typedValue = JsonValueConverter.ConvertValue(field, jsonValue);
                    }
                }

                if (validators?.Length > 0)
                {
                    var tasks = new List <Task>();

                    foreach (var validator in validators)
                    {
                        tasks.Add(validator.ValidateAsync(typedValue, context, addError));
                    }

                    await Task.WhenAll(tasks);
                }
            }
            catch
            {
                addError(context.Path, "Not a valid value.");
            }
        }
예제 #10
0
        public Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value is TValue typedValue)
            {
                if (min != null && max != null)
                {
                    if (Equals(min, max) && Equals(min.Value, max.Value))
                    {
                        addError(context.Path, T.Get("contents.validation.exactValue", new { value = max.Value }));
                    }
                    else if (typedValue.CompareTo(min.Value) < 0 || typedValue.CompareTo(max.Value) > 0)
                    {
                        addError(context.Path, T.Get("contents.validation.between", new { min, max }));
                    }
                }
                else
                {
                    if (min != null && typedValue.CompareTo(min.Value) < 0)
                    {
                        addError(context.Path, T.Get("contents.validation.min", new { min }));
                    }

                    if (max != null && typedValue.CompareTo(max.Value) > 0)
                    {
                        addError(context.Path, T.Get("contents.validation.max", new { max }));
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #11
0
        private void ValidateDimensions(int w, int h, ImmutableQueue <string> path, AddError addError)
        {
            var actualRatio = (double)w / h;

            if (properties.MinWidth != null && w < properties.MinWidth)
            {
                addError(path, T.Get("contents.validation.minimumWidth", new { width = w, min = properties.MinWidth }));
            }

            if (properties.MaxWidth != null && w > properties.MaxWidth)
            {
                addError(path, T.Get("contents.validation.maximumWidth", new { width = w, max = properties.MaxWidth }));
            }

            if (properties.MinHeight != null && h < properties.MinHeight)
            {
                addError(path, T.Get("contents.validation.minimumHeight", new { height = h, min = properties.MinHeight }));
            }

            if (properties.MaxHeight != null && h > properties.MaxHeight)
            {
                addError(path, T.Get("contents.validation.maximumHeight", new { height = h, max = properties.MaxHeight }));
            }

            if (properties.AspectHeight != null && properties.AspectWidth != null)
            {
                var expectedRatio = (double)properties.AspectWidth.Value / properties.AspectHeight.Value;

                if (Math.Abs(expectedRatio - actualRatio) > double.Epsilon)
                {
                    addError(path, T.Get("contents.validation.aspectRatio", new { width = properties.AspectWidth, height = properties.AspectHeight }));
                }
            }
        }
예제 #12
0
        public Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value is string stringValue && !string.IsNullOrEmpty(stringValue))
            {
                if (minLength != null && maxLength != null)
                {
                    if (minLength == maxLength && minLength != stringValue.Length)
                    {
                        addError(context.Path, T.Get("contents.validation.characterCount", new { count = minLength }));
                    }
                    else if (stringValue.Length < minLength || stringValue.Length > maxLength)
                    {
                        addError(context.Path, T.Get("contents.validation.charactersBetween", new { min = minLength, max = maxLength }));
                    }
                }
                else
                {
                    if (minLength != null && stringValue.Length < minLength)
                    {
                        addError(context.Path, T.Get("contents.validation.minLength", new { min = minLength }));
                    }

                    if (maxLength != null && stringValue.Length > maxLength)
                    {
                        addError(context.Path, T.Get("contents.validation.maxLength", new { max = maxLength }));
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #13
0
        public Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value != null && value is T typedValue)
            {
                if (min.HasValue && max.HasValue)
                {
                    if (Equals(min, max) && Equals(min.Value, max.Value))
                    {
                        addError(context.Path, $"Must be exactly '{max}'.");
                    }
                    else if (typedValue.CompareTo(min.Value) < 0 || typedValue.CompareTo(max.Value) > 0)
                    {
                        addError(context.Path, $"Must be between '{min}' and '{max}'.");
                    }
                }
                else
                {
                    if (min.HasValue && typedValue.CompareTo(min.Value) < 0)
                    {
                        addError(context.Path, $"Must be greater or equal to '{min}'.");
                    }

                    if (max.HasValue && typedValue.CompareTo(max.Value) > 0)
                    {
                        addError(context.Path, $"Must be less or equal to '{max}'.");
                    }
                }
            }

            return(TaskHelper.Done);
        }
예제 #14
0
        public async Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            try
            {
                object typedValue = null;

                if (value is JToken jToken)
                {
                    typedValue = jToken.IsNull() ? null : JsonValueConverter.ConvertValue(field, jToken);
                }

                var tasks = new List <Task>();

                foreach (var validator in validators)
                {
                    tasks.Add(validator.ValidateAsync(typedValue, context, addError));
                }

                await Task.WhenAll(tasks);
            }
            catch
            {
                addError(context.Path, "Not a valid value.");
            }
        }
예제 #15
0
        public async Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            var typedValue = value;

            try
            {
                if (value is IJsonValue jsonValue)
                {
                    if (jsonValue.Type == JsonValueType.Null)
                    {
                        typedValue = null;
                    }
                    else
                    {
                        var(json, error) = JsonValueConverter.ConvertValue(field, jsonValue);

                        if (error != null)
                        {
                            addError(context.Path, error.Error);
                        }
                        else
                        {
                            typedValue = json;
                        }
                    }
                }
            }
            catch
            {
                addError(context.Path, T.Get("contents.validation.invalid"));
                return;
            }

            await fieldValueValidator.ValidateAsync(typedValue, context, addError);
        }
예제 #16
0
        public Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            if (value is string stringValue)
            {
                if (!string.IsNullOrEmpty(stringValue))
                {
                    try
                    {
                        if (!regex.IsMatch(stringValue))
                        {
                            if (string.IsNullOrWhiteSpace(errorMessage))
                            {
                                addError(context.Path, "Not valid.");
                            }
                            else
                            {
                                addError(context.Path, errorMessage);
                            }
                        }
                    }
                    catch
                    {
                        addError(context.Path, "Regex is too slow.");
                    }
                }
            }

            return(TaskHelper.Done);
        }
예제 #17
0
        public Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value is string stringValue)
            {
                if (!string.IsNullOrEmpty(stringValue))
                {
                    try
                    {
                        if (!regex.IsMatch(stringValue))
                        {
                            if (string.IsNullOrWhiteSpace(errorMessage))
                            {
                                addError(context.Path, T.Get("contents.validation.pattern"));
                            }
                            else
                            {
                                addError(context.Path, errorMessage);
                            }
                        }
                    }
                    catch
                    {
                        addError(context.Path, T.Get("contents.validation.regexTooSlow"));
                    }
                }
            }

            return(Task.CompletedTask);
        }
예제 #18
0
        public async Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            if (value is ContentData data)
            {
                var validateableFields = context.Schema.Fields.Where(IsValidateableField);

                var filters = new List <FilterNode <ClrValue> >();

                foreach (var field in validateableFields)
                {
                    var fieldValue = TryGetValue(field, data);

                    if (fieldValue != null)
                    {
                        filters.Add(ClrFilter.Eq($"data.{field.Name}.iv", fieldValue));
                    }
                }

                if (filters.Count > 0)
                {
                    var filter = ClrFilter.And(filters);

                    var found = await contentRepository.QueryIdsAsync(context.AppId.Id, context.SchemaId.Id, filter);

                    if (found.Any(x => x.Id != context.ContentId))
                    {
                        addError(Enumerable.Empty <string>(), "A content with the same values already exist.");
                    }
                }
            }
        }
예제 #19
0
        public async Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (context.Mode == ValidationMode.Optimized)
            {
                return;
            }

            if (value is ICollection <Guid> contentIds)
            {
                var foundIds = await context.GetContentIdsAsync(contentIds.ToHashSet());

                foreach (var id in contentIds)
                {
                    var(schemaId, _) = foundIds.FirstOrDefault(x => x.Id == id);

                    if (schemaId == Guid.Empty)
                    {
                        addError(context.Path, $"Contains invalid reference '{id}'.");
                    }
                    else if (schemaIds?.Any() == true && !schemaIds.Contains(schemaId))
                    {
                        addError(context.Path, $"Contains reference '{id}' to invalid schema.");
                    }
                }
            }
        }
예제 #20
0
        public async Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            var count = context.Path.Count();

            if (value != null && (count == 0 || (count == 2 && context.Path.Last() == InvariantPartitioning.Key)))
            {
                FilterNode <ClrValue> filter = null;

                if (value is string s)
                {
                    filter = ClrFilter.Eq(Path(context), s);
                }
                else if (value is double d)
                {
                    filter = ClrFilter.Eq(Path(context), d);
                }

                if (filter != null)
                {
                    var found = await context.GetContentIdsAsync(context.SchemaId, filter);

                    if (found.Any(x => x != context.ContentId))
                    {
                        addError(context.Path, "Another content with the same value exists.");
                    }
                }
            }
        }
 static void AddErrors(AddError addError, IEnumerable<string> messages)
 {
     foreach (string message in messages) {
         BuildError error = CreateBuildError (message);
         addError (error.FileName, error.Line, error.Column, error.ErrorNumber, error.ErrorText);
     }
 }
예제 #22
0
        public async Task ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            var foundIds = new List <DomainId>();

            if (value is ICollection <DomainId> contentIds && contentIds.Count > 0)
            {
                var references = await checkReferences(contentIds.ToHashSet());

                var index = 0;

                foreach (var id in contentIds)
                {
                    index++;

                    var path = context.Path.Enqueue($"[{index}]");

                    var(schemaId, _, status) = references.FirstOrDefault(x => x.Id == id);

                    if (schemaId == DomainId.Empty)
                    {
                        if (context.Action == ValidationAction.Upsert)
                        {
                            addError(path, T.Get("contents.validation.referenceNotFound", new { id }));
                        }

                        continue;
                    }

                    var isValid = true;

                    if (properties.SchemaIds?.Any() == true && !properties.SchemaIds.Contains(schemaId))
                    {
                        if (context.Action == ValidationAction.Upsert)
                        {
                            addError(path, T.Get("contents.validation.referenceToInvalidSchema", new { id }));
                        }

                        isValid = false;
                    }

                    isValid &= (!properties.MustBePublished || status == Status.Published);

                    if (isValid)
                    {
                        foundIds.Add(id);
                    }
                }
            }

            if (collectionValidator != null)
            {
                await collectionValidator.ValidateAsync(foundIds, context, addError);
            }

            if (uniqueValidator != null)
            {
                await uniqueValidator.ValidateAsync(foundIds, context, addError);
            }
        }
예제 #23
0
        public ValueTask ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value.IsNullOrUndefined() && !context.IsOptional)
            {
                addError(context.Path, T.Get("contents.validation.required"));
            }

            return(default);
예제 #24
0
        public ValueTask ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (!value.IsUndefined())
            {
                addError(context.Path, T.Get("contents.validation.mustBeEmpty"));
            }

            return(default);
 static void AddErrors(AddError addError, IEnumerable <string> messages)
 {
     foreach (string message in messages)
     {
         BuildError error = CreateBuildError(message);
         addError(error.FileName, error.Line, error.Column, error.ErrorNumber, error.ErrorText);
     }
 }
예제 #26
0
        public ValueTask ValidateAsync(object?value, ValidationContext context, AddError addError)
        {
            if (value is TValue typedValue && !allowedValues.Contains(typedValue))
            {
                addError(context.Path, T.Get("contents.validation.notAllowed"));
            }

            return(default);
예제 #27
0
        public Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            if (validators?.Length > 0)
            {
                return(Task.WhenAll(validators.Select(x => x.ValidateAsync(value, context, addError))));
            }

            return(Task.CompletedTask);
        }
예제 #28
0
        private void ValidateType(IAssetInfo asset, ImmutableQueue <string> path, AddError addError)
        {
            var type = asset.MimeType == "image/svg+xml" ? AssetType.Image : asset.Type;

            if (properties.ExpectedType != null && properties.ExpectedType != type)
            {
                addError(path, T.Get("contents.validation.assetType", new { type = properties.ExpectedType }));
            }
        }
예제 #29
0
        public Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            if (!value.IsUndefined())
            {
                addError(context.Path, "Value must not be defined.");
            }

            return(Task.CompletedTask);
        }
예제 #30
0
        public Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            if (value.IsNullOrUndefined() && !context.IsOptional)
            {
                addError(context.Path, "Field is required.");
            }

            return(TaskHelper.Done);
        }
예제 #31
0
        public Task ValidateAsync(object value, ValidationContext context, AddError addError)
        {
            if (value != null && value is T typedValue && !allowedValues.Contains(typedValue))
            {
                addError(context.Path, "Not an allowed value.");
            }

            return(TaskHelper.Done);
        }