예제 #1
0
        private void ValidateString <T>(JToken node, ICollection <string> errorMessages) where T : KongObject
        {
            var value = (string)node;

            if (OneOf.Any())
            {
                var options = OneOf.Select(x => x.Value <string>()).ToArray();
                if (!options.Contains(value))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be one of '{string.Join(", ", options)}').");
                }
            }
            if (StartsWith != null)
            {
                if (value == null || !value.StartsWith(StartsWith))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should start with '{StartsWith}').");
                }
            }
            if (Match != null)
            {
                if (!LuaPattern.IsMatch(value, Match))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should match lua pattern '{Match}').");
                }
            }
            if (MatchAny != null)
            {
                if (MatchAny.Patterns.All(x => !LuaPattern.IsMatch(value, x)))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' {MatchAny.Error}).");
                }
            }
            if (MatchNone.Any())
            {
                foreach (var fieldCheck in MatchNone)
                {
                    if (LuaPattern.IsMatch(value, fieldCheck.Pattern))
                    {
                        errorMessages.Add($"{Violation<T>()} (field '{node.Path}' {fieldCheck.Error}).");
                    }
                }
            }
            if (MatchAll.Any())
            {
                foreach (var fieldCheck in MatchAll)
                {
                    if (!LuaPattern.IsMatch(value, fieldCheck.Pattern))
                    {
                        errorMessages.Add($"{Violation<T>()} (field '{node.Path}' {fieldCheck.Error}).");
                    }
                }
            }
            if (LenMin.HasValue)
            {
                var len = value?.Length;
                if (len < LenMin)
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should have min length '{LenMin.Value}').");
                }
            }
            if (Uuid)
            {
                if (!Guid.TryParse(value, out _))
                {
                    errorMessages.Add($"{Violation<T>()} (field '{node.Path}' should be a 'uuid').");
                }
            }
        }
예제 #2
0
 /// <summary>
 /// Initializes an instance of <see cref="IncludeNone"/>.
 /// </summary>
 public IncludeNone()
 {
     Specification = new MatchNone<string>();
 }