Exemplo n.º 1
0
        protected void EnsureEnum(JsonToken token, object value)
        {
            bool isEnum       = !Schema._enum.IsNullOrEmpty();
            bool hasConst     = Schema.Const != null;
            bool hasValidator = !Schema._validators.IsNullOrEmpty();

            if (isEnum || hasConst || hasValidator)
            {
                if (JsonTokenHelpers.IsPrimitiveOrStartToken(token))
                {
                    if (Context.TokenWriter == null)
                    {
                        Context.TokenWriter = new JTokenWriter();
                        Context.TokenWriter.WriteToken(token, value);
                    }
                }

                if (JsonTokenHelpers.IsPrimitiveOrEndToken(token))
                {
                    JToken currentToken = Context.TokenWriter.CurrentToken;

                    if (isEnum)
                    {
                        bool defined = JsonTokenHelpers.Contains(Schema._enum, currentToken);

                        if (!defined)
                        {
                            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                            currentToken.WriteTo(new JsonTextWriter(sw));

                            RaiseError($"Value {sw.ToString()} is not defined in enum.", ErrorType.Enum, Schema, value, null);
                        }
                    }

                    if (hasConst)
                    {
                        bool defined = Schema.Const.DeepEquals(currentToken);

                        if (!defined)
                        {
                            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                            currentToken.WriteTo(new JsonTextWriter(sw));

                            RaiseError($"Value {sw.ToString()} does not match const.", ErrorType.Const, Schema, value, null);
                        }
                    }

                    if (hasValidator)
                    {
                        JsonValidatorContext context = new JsonValidatorContext(this, Schema);

                        foreach (JsonValidator validator in Schema._validators)
                        {
                            validator.Validate(currentToken, context);
                        }
                    }
                }
            }
        }
 public override void Validate(JToken value, JsonValidatorContext context)
 {
     // we should ignore the "root token validation"
     if (!context.Schema.ExtensionData.ContainsKey("greaterthanfield"))
     {
         return;
     }
     // value.Parent is hydrated now
 }
Exemplo n.º 3
0
            public override void Validate(JToken value, JsonValidatorContext context)
            {
                var groupedValues = value.GroupBy(v => v["key"], v => v["value"], JToken.EqualityComparer);

                foreach (var groupedValue in groupedValues)
                {
                    if (groupedValue.Count() > 1)
                    {
                        context.RaiseError($"Duplicate key: {groupedValue.Key.ToString(Formatting.None)}");
                    }
                }
            }
            public override void Validate(JToken value, JsonValidatorContext context)
            {
                var groupedValues = value.GroupBy(v => v["key"], v => v["value"], JToken.EqualityComparer);

                foreach (var groupedValue in groupedValues)
                {
                    if (groupedValue.Count() > 1)
                    {
                        context.RaiseError($"Duplicate key: {groupedValue.Key.ToString(Formatting.None)}");
                    }
                }
            }
    public override void Validate(JToken value, JsonValidatorContext context)
    {
        if (value.Type != JTokenType.String)
        {
            return;
        }
        var      stringValue = value.ToString();
        DateTime date;

        if (!DateTime.TryParseExact(stringValue, "dd/MM/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out date))
        {
            context.RaiseError($"Text '{stringValue}' is not a valid date.");
        }
    }
Exemplo n.º 6
0
            public override void Validate(JToken value, JsonValidatorContext context)
            {
                if (value.Type == JTokenType.String)
                {
                    string s = value.ToString();

                    try
                    {
                        JToken.Parse(s);
                    }
                    catch (Exception ex)
                    {
                        context.RaiseError($"String is not JSON: {ex.Message}");
                    }
                }
            }
            public override void Validate(JToken value, JsonValidatorContext context)
            {
                if (value.Type == JTokenType.String)
                {
                    string s = value.ToString();

                    try
                    {
                        // test whether the string is a known culture, e.g. en-US, fr-FR
                        CultureInfo.GetCultureInfo(s);
                    }
                    catch (CultureNotFoundException)
                    {
                        context.RaiseError($"Text '{s}' is not a valid culture name.");
                    }
                }
            }
            public override void Validate(JToken value, JsonValidatorContext context)
            {
                if (value.Type == JTokenType.String)
                {
                    string s = value.ToString();

                    try
                    {
                        // test whether the string is a known culture, e.g. en-US, fr-FR
                        new CultureInfo(s);
                    }
                    catch (CultureNotFoundException)
                    {
                        context.RaiseError($"Text '{s}' is not a valid culture name.");
                    }
                }
            }
        protected void EnsureEnum(JsonToken token, object?value)
        {
            bool isEnum       = !Schema._enum.IsNullOrEmpty();
            bool hasConst     = Schema.Const != null;
            bool hasValidator = !Schema._validators.IsNullOrEmpty();

            if (isEnum || hasConst || hasValidator)
            {
                if (JsonTokenHelpers.IsPrimitiveOrStartToken(token))
                {
                    if (Context.TokenWriter == null)
                    {
                        Context.TokenWriter = new JTokenWriter();
                        Context.TokenWriter.WriteToken(token, value);
                    }
                }
                else if (token == JsonToken.PropertyName)
                {
                    if (Context.TokenWriter == null)
                    {
                        Context.TokenWriter = new JTokenWriter();
                        Context.TokenWriter.WriteToken(JsonToken.String, value);
                    }
                }

                if (JsonTokenHelpers.IsPrimitiveOrEndToken(token) || token == JsonToken.PropertyName)
                {
                    ValidationUtils.Assert(Context.TokenWriter != null);

                    JToken currentToken = Context.TokenWriter.CurrentToken !;

                    if (isEnum)
                    {
                        JToken resolvedToken = (currentToken is JProperty property)
                            ? new JValue(property.Name)
                            : currentToken;

                        bool defined = JsonTokenHelpers.Contains(Schema._enum !, resolvedToken);

                        if (!defined)
                        {
                            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                            currentToken.WriteTo(new JsonTextWriter(sw));

                            RaiseError($"Value {sw.ToString()} is not defined in enum.", ErrorType.Enum, Schema, value, null);
                        }
                    }

                    if (hasConst)
                    {
                        bool defined = JsonTokenHelpers.ImplicitDeepEquals(Schema.Const !, currentToken);

                        if (!defined)
                        {
                            StringWriter sw = new StringWriter(CultureInfo.InvariantCulture);
                            currentToken.WriteTo(new JsonTextWriter(sw));

                            RaiseError($"Value {sw.ToString()} does not match const.", ErrorType.Const, Schema, value, null);
                        }
                    }

                    if (hasValidator)
                    {
                        JsonValidatorContext context = new JsonValidatorContext(this, Schema);

                        foreach (JsonValidator validator in Schema._validators !)
                        {
                            validator.Validate(currentToken, context);
                        }
                    }
                }
            }
        }
 public override void Validate(JToken value, JsonValidatorContext context)
 {
 }
            public override void Validate(JToken value, JsonValidatorContext context)
            {
                if (value.Type == JTokenType.String)
                {
                    string s = value.ToString();

                    try
                    {
                        JToken.Parse(s);
                    }
                    catch (Exception ex)
                    {
                        context.RaiseError($"String is not JSON: {ex.Message}");
                    }
                }
            }