Represents a reader that provides JsonSchema validation.
Наследование: JsonReader, IJsonLineInfo
        /// <summary>
        /// Verifies Json literal content 
        /// </summary>
        /// <param name="content">the Json literal to be verified</param>
        /// <param name="result">output paramter of test result</param>
        /// <returns>true if verification passes; false otherwiser</returns>
        public bool Verify(string content, out TestResult result)
        {
            using (var stringReader = new StringReader(content))
            {
                using (JsonTextReader rdr = new JsonTextReader(stringReader))
                {
                    using (JsonValidatingReader vr = new JsonValidatingReader(rdr))
                    {
                        vr.Schema = this.schema;

                        try
                        {
                            while (vr.Read())
                            {
                                // Ignore
                            }

                            result = new TestResult();
                            return true;
                        }
                        catch (JsonSchemaException jex)
                        {
                            result = new TestResult() { LineNumberInError = jex.LineNumber, ErrorDetail = jex.Message };
                            return false;
                        }
                    }
                }
            }
        }
Пример #2
0
        private void ValidateFloat(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }
            if (!this.TestType(schema, JsonSchemaType.Float))
            {
                return;
            }
            this.ValidateInEnumAndNotDisallowed(schema);
            double num = Convert.ToDouble(this._reader.Value, CultureInfo.InvariantCulture);

            if (schema.Maximum.HasValue)
            {
                double?maximum = schema.Maximum;
                if ((!maximum.HasValue ? false : num > maximum.GetValueOrDefault()))
                {
                    this.RaiseError("Float {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, new object[] { JsonConvert.ToString(num), schema.Maximum }), schema);
                }
                if (schema.ExclusiveMaximum)
                {
                    double?nullable = schema.Maximum;
                    if ((num != nullable.GetValueOrDefault() ? false : nullable.HasValue))
                    {
                        this.RaiseError("Float {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, new object[] { JsonConvert.ToString(num), schema.Maximum }), schema);
                    }
                }
            }
            if (schema.Minimum.HasValue)
            {
                double?minimum = schema.Minimum;
                if ((!minimum.HasValue ? false : num < minimum.GetValueOrDefault()))
                {
                    this.RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, new object[] { JsonConvert.ToString(num), schema.Minimum }), schema);
                }
                if (schema.ExclusiveMinimum)
                {
                    double?minimum1 = schema.Minimum;
                    if ((num != minimum1.GetValueOrDefault() ? false : minimum1.HasValue))
                    {
                        this.RaiseError("Float {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, new object[] { JsonConvert.ToString(num), schema.Minimum }), schema);
                    }
                }
            }
            if (schema.DivisibleBy.HasValue && !JsonValidatingReader.IsZero(num % schema.DivisibleBy.Value))
            {
                this.RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, new object[] { JsonConvert.ToString(num), schema.DivisibleBy }), schema);
            }
        }
Пример #3
0
 public static void Validate(this JToken source, JsonSchema schema, ValidationEventHandler validationEventHandler)
 {
   ValidationUtils.ArgumentNotNull((object) source, "source");
   ValidationUtils.ArgumentNotNull((object) schema, "schema");
   using (JsonValidatingReader validatingReader = new JsonValidatingReader(source.CreateReader()))
   {
     validatingReader.Schema = schema;
     if (validationEventHandler != null)
       validatingReader.ValidationEventHandler += validationEventHandler;
     do
       ;
     while (validatingReader.Read());
   }
 }
        public async Task<HttpResponseMessage> PostData(HttpRequestMessage request)
        {
            try
            {
                var jsonString = await request.Content.ReadAsStringAsync();

                //Validate JSON with JsonValidatingReader 
                JsonTextReader reader = new JsonTextReader(new StringReader(jsonString));
                JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
                validatingReader.Schema = JsonSchema.Parse(Helper.Instance().jsonRequest);

                IList<string> messages = new List<string>();
                validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);
                JsonSerializer serializer = new JsonSerializer();

                //DeserializeObject
                RequestData requestData = serializer.Deserialize<RequestData>(validatingReader);

                //Compute responseData
                var responseDatas = Helper.Instance().GetResponseData(requestData);


                //
                HttpResponseMessage response = new HttpResponseMessage();
                response.StatusCode = HttpStatusCode.OK;
                string jsonResponse = JsonConvert.SerializeObject(responseDatas);
                response.Content = new StringContent(jsonResponse, Encoding.UTF8, "application/json");

                response.Headers.CacheControl = new CacheControlHeaderValue()
                {
                    MaxAge = TimeSpan.FromMinutes(20)
                };

                return response;
            }
            catch
            {
                var myError = new
                {
                    //error = ex.Message
                    error = "Could not decode request: JSON parsing failed"
                };

                return Request.CreateResponse(HttpStatusCode.BadRequest, myError);

            }
        }
Пример #5
0
        private void ValidateInteger(JsonSchemaModel schema)
        {
            double?divisibleBy;
            bool   bigInteger;

            if (schema == null)
            {
                return;
            }
            if (!this.TestType(schema, JsonSchemaType.Integer))
            {
                return;
            }
            this.ValidateNotDisallowed(schema);
            object value = this._reader.Value;

            if (schema.Maximum.HasValue)
            {
                if (JValue.Compare(JTokenType.Integer, value, schema.Maximum) > 0)
                {
                    this.RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
                if (schema.ExclusiveMaximum && JValue.Compare(JTokenType.Integer, value, schema.Maximum) == 0)
                {
                    this.RaiseError("Integer {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
            }
            if (schema.Minimum.HasValue)
            {
                if (JValue.Compare(JTokenType.Integer, value, schema.Minimum) < 0)
                {
                    this.RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
                if (schema.ExclusiveMinimum && JValue.Compare(JTokenType.Integer, value, schema.Minimum) == 0)
                {
                    this.RaiseError("Integer {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
            }
            if (schema.DivisibleBy.HasValue)
            {
                object obj  = value;
                object obj1 = obj;
                if (!(obj is BigInteger))
                {
                    double num = (double)Convert.ToInt64(value, CultureInfo.InvariantCulture);
                    divisibleBy = schema.DivisibleBy;
                    bigInteger  = !JsonValidatingReader.IsZero(num % divisibleBy.GetValueOrDefault());
                }
                else
                {
                    BigInteger bigInteger1 = (BigInteger)obj1;
                    if (Math.Abs(schema.DivisibleBy.Value - Math.Truncate(schema.DivisibleBy.Value)).Equals(0))
                    {
                        divisibleBy = schema.DivisibleBy;
                        bigInteger  = (bigInteger1 % new BigInteger(divisibleBy.Value)) != 0L;
                    }
                    else
                    {
                        bigInteger = bigInteger1 != 0L;
                    }
                }
                if (bigInteger)
                {
                    this.RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
                }
            }
        }
Пример #6
0
 private void Push(JsonValidatingReader.SchemaScope scope)
 {
   this._stack.Push(scope);
   this._currentScope = scope;
 }
Пример #7
0
        public Manifest GetManifestFromRemote()
        {
            FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(Assembly.GetExecutingAssembly().Location);
            string jsonStr = wc.DownloadString("catflap.json?catflap=" + fvi.FileVersion);
            JsonTextReader reader = new JsonTextReader(new StringReader(jsonStr));

            JsonValidatingReader validatingReader = new JsonValidatingReader(reader);
            validatingReader.Schema = Manifest.Schema;
            // validatingReader.Schema.AllowAdditionalItems = false;
            // validatingReader.Schema.AllowAdditionalProperties = false;
            IList<string> messages = new List<string>();
            validatingReader.ValidationEventHandler += (o, a) => messages.Add(a.Message);

            JsonSerializer serializer = new JsonSerializer();
            Manifest mf = serializer.Deserialize<Manifest>(validatingReader);

            if (messages.Count > 0)
                throw new ValidationException("manifesto não é válido: " + string.Join("\n", messages));

            mf.Validate(RootPath);

            return mf;
        }
Пример #8
0
        private void ValidateFloat(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }
            if (!this.TestType(schema, JsonSchemaType.Float))
            {
                return;
            }
            this.ValidateInEnumAndNotDisallowed(schema);
            double num = Convert.ToDouble(this._reader.Value, CultureInfo.get_InvariantCulture());

            if (schema.Maximum.get_HasValue())
            {
                double?maximum = schema.Maximum;
                if (maximum.get_HasValue() && num > maximum.get_Value())
                {
                    this.RaiseError("Float {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                    {
                        JsonConvert.ToString(num),
                        schema.Maximum
                    }), schema);
                }
                if (schema.ExclusiveMaximum)
                {
                    double arg_B7_0 = num;
                    double?maximum2 = schema.Maximum;
                    if (arg_B7_0 == maximum2.GetValueOrDefault() && maximum2.get_HasValue())
                    {
                        this.RaiseError("Float {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                        {
                            JsonConvert.ToString(num),
                            schema.Maximum
                        }), schema);
                    }
                }
            }
            if (schema.Minimum.get_HasValue())
            {
                double?minimum = schema.Minimum;
                if (minimum.get_HasValue() && num < minimum.get_Value())
                {
                    this.RaiseError("Float {0} is less than minimum value of {1}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                    {
                        JsonConvert.ToString(num),
                        schema.Minimum
                    }), schema);
                }
                if (schema.ExclusiveMinimum)
                {
                    double arg_183_0 = num;
                    double?minimum2  = schema.Minimum;
                    if (arg_183_0 == minimum2.GetValueOrDefault() && minimum2.get_HasValue())
                    {
                        this.RaiseError("Float {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                        {
                            JsonConvert.ToString(num),
                            schema.Minimum
                        }), schema);
                    }
                }
            }
            if (schema.DivisibleBy.get_HasValue() && !JsonValidatingReader.IsZero(num % schema.DivisibleBy.get_Value()))
            {
                this.RaiseError("Float {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
                {
                    JsonConvert.ToString(num),
                    schema.DivisibleBy
                }), schema);
            }
        }
        private void ValidateInteger(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }
            if (!this.TestType(schema, JsonSchemaType.Integer))
            {
                return;
            }
            this.ValidateInEnumAndNotDisallowed(schema);
            long num = Convert.ToInt64(this._reader.Value, CultureInfo.InvariantCulture);

            if (schema.Maximum != null)
            {
                double?maximum = schema.Maximum;
                if ((double)num > maximum)
                {
                    this.RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        num,
                        schema.Maximum
                    }), schema);
                }
                if (schema.ExclusiveMaximum && (double)num == schema.Maximum)
                {
                    this.RaiseError("Integer {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        num,
                        schema.Maximum
                    }), schema);
                }
            }
            if (schema.Minimum != null)
            {
                double?minimum = schema.Minimum;
                if ((double)num < minimum)
                {
                    this.RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        num,
                        schema.Minimum
                    }), schema);
                }
                if (schema.ExclusiveMinimum && (double)num == schema.Minimum)
                {
                    this.RaiseError("Integer {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, new object[]
                    {
                        num,
                        schema.Minimum
                    }), schema);
                }
            }
            if (schema.DivisibleBy != null && !JsonValidatingReader.IsZero((double)num % schema.DivisibleBy.Value))
            {
                this.RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, new object[]
                {
                    JsonConvert.ToString(num),
                    schema.DivisibleBy
                }), schema);
            }
        }
Пример #10
0
        private void ValidateInteger(JsonSchemaModel schema)
        {
            if (schema == null)
            {
                return;
            }
            if (!this.TestType(schema, JsonSchemaType.Integer))
            {
                return;
            }
            this.ValidateNotDisallowed(schema);
            object value = this._reader.Value;

            if (schema.Maximum.HasValue)
            {
                if (JValue.Compare(JTokenType.Integer, value, schema.Maximum) > 0)
                {
                    this.RaiseError("Integer {0} exceeds maximum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
                if (schema.ExclusiveMaximum && JValue.Compare(JTokenType.Integer, value, schema.Maximum) == 0)
                {
                    this.RaiseError("Integer {0} equals maximum value of {1} and exclusive maximum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Maximum), schema);
                }
            }
            if (schema.Minimum.HasValue)
            {
                if (JValue.Compare(JTokenType.Integer, value, schema.Minimum) < 0)
                {
                    this.RaiseError("Integer {0} is less than minimum value of {1}.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
                if (schema.ExclusiveMinimum && JValue.Compare(JTokenType.Integer, value, schema.Minimum) == 0)
                {
                    this.RaiseError("Integer {0} equals minimum value of {1} and exclusive minimum is true.".FormatWith(CultureInfo.InvariantCulture, value, schema.Minimum), schema);
                }
            }
            if (schema.DivisibleBy.HasValue)
            {
                bool flag2;
                if (value is BigInteger)
                {
                    BigInteger bigInteger = (BigInteger)value;
                    bool       flag       = !Math.Abs(schema.DivisibleBy.Value - Math.Truncate(schema.DivisibleBy.Value)).Equals(0.0);
                    if (flag)
                    {
                        flag2 = (bigInteger != 0L);
                    }
                    else
                    {
                        flag2 = (bigInteger % new BigInteger(schema.DivisibleBy.Value) != 0L);
                    }
                }
                else
                {
                    flag2 = !JsonValidatingReader.IsZero((double)Convert.ToInt64(value, CultureInfo.InvariantCulture) % schema.DivisibleBy.Value);
                }
                if (flag2)
                {
                    this.RaiseError("Integer {0} is not evenly divisible by {1}.".FormatWith(CultureInfo.InvariantCulture, JsonConvert.ToString(value), schema.DivisibleBy), schema);
                }
            }
        }