Пример #1
0
        public void Hardcoded(IJsonSchema schema)
        {
            var json       = schema.ToJson(null);
            var validation = schema.Validate(json);

            validation.AssertValid();
        }
Пример #2
0
        public void Online(IJsonSchema schema)
        {
            try
            {
                // TODO: Catch web exceptions and assert inconclusive.
                var localSchemaJson = schema.ToJson(null);

                var onlineSchemaText = JsonSchemaOptions.Download(schema.Id);
                var onlineSchemaJson = JsonValue.Parse(onlineSchemaText);
                var onlineSchema     = JsonSchemaFactory.FromJson(onlineSchemaJson);

                var localValidation  = schema.Validate(onlineSchemaJson);
                var onlineValidation = onlineSchema.Validate(localSchemaJson);

                Assert.AreEqual(onlineSchema, schema);

                onlineValidation.AssertValid();
                localValidation.AssertValid();

                Assert.AreEqual(onlineSchemaJson, localSchemaJson);
            }
            catch (WebException)
            {
                Assert.Inconclusive();
            }
            catch (AggregateException e)
            {
                if (e.InnerExceptions.OfType <WebException>().Any())
                {
                    Assert.Inconclusive();
                }
                throw;
            }
        }
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var objectModel = JsonConvert.SerializeObject(context.ActionArguments.FirstOrDefault().Value,
                                                          new JsonSerializerSettings {
                ContractResolver = new CamelCasePropertyNamesContractResolver(), NullValueHandling = NullValueHandling.Ignore
            });

            if (string.IsNullOrEmpty(objectModel))
            {
                _logger.LogWarning("Object not defined.");
                context.Result = new BadRequestResult();
                return;
            }

            var response = _schema.Validate(JsonValue.Parse(objectModel));

            if (!response.Valid)
            {
                var errors       = response.Errors.Select(e => e.Message + " " + e.PropertyName).ToList();
                var errorMessage = errors.Aggregate("", (s, i) => "" + s + "," + i);
                _logger.LogWarning($"Errors during JSON schema validation: {errorMessage}");
                context.Result = new BadRequestObjectResult(new { error = errors });
                return;
            }

            await base.OnActionExecutionAsync(context, next);
        }
Пример #4
0
 /// <summary>
 /// Validates a <see cref="JsonValue"/> against the schema.
 /// </summary>
 /// <param name="json">A <see cref="JsonValue"/></param>
 /// <param name="root">The root schema serialized to a <see cref="JsonValue"/>.  Used internally for resolving references.</param>
 /// <returns>The results of the validation.</returns>
 public SchemaValidationResults Validate(JsonValue json, JsonValue root = null)
 {
     if (json.Type != JsonValueType.Object || !json.Object.ContainsKey(PropertyName))
     {
         return(new SchemaValidationResults());
     }
     return(_schema.Validate(json, root));
 }
Пример #5
0
        public void ValidateReturnsErrorOnNoneValid(IJsonSchema schema)
        {
            var json = new JsonObject();

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #6
0
        public void ValidateReturnsValidOnLessThanMaximum(IJsonSchema schema)
        {
            var json = (JsonValue)3;

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #7
0
        public void ValidateReturnsValidOnBoolean(IJsonSchema schema)
        {
            var json = (JsonValue)false;

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #8
0
        public void ValidateReturnsErrorOnMoreThanMaximum(IJsonSchema schema)
        {
            var json = (JsonValue)10;

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #9
0
        public void ValidateReturnsValidOnMultipleOf_Negative(IJsonSchema schema)
        {
            var json = (JsonValue)(-15);

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #10
0
        public void ValidateReturnsErrorOnLessThanMinimum(IJsonSchema schema)
        {
            var json = (JsonValue)4;

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #11
0
        public void ValidateReturnsValidOnMoreThanMinimum(IJsonSchema schema)
        {
            var json = (JsonValue)10;

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #12
0
        public void ValidateReturnsErrorOnString(IJsonSchema schema)
        {
            JsonValue json = "string";

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #13
0
        public void ValidateReturnsErrorOnNonInteger(IJsonSchema schema)
        {
            var json = (JsonValue)1.2;

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #14
0
        public void ValidateReturnsValidOnMultipleOf_Zero(IJsonSchema schema)
        {
            var json = (JsonValue)0;

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #15
0
        public void ValidateReturnsValidOnNull(IJsonSchema schema)
        {
            var json = JsonValue.Null;

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #16
0
        public void ValidateReturnsValidOnSingleValid(IJsonSchema schema)
        {
            var json = (JsonValue)25;

            var results = schema.Validate(json);

            results.AssertValid();
        }
        public JsonSchemaValidationResult Validate(string json)
        {
            var result = _jsonSchema.Validate(JsonValue.Parse(json));

            return(result.Valid
                ? JsonSchemaValidationResult.Success()
                : JsonSchemaValidationResult.FromError(result.Errors));
        }
Пример #18
0
        public void ValidateReturnsValidOnValueInRange(IJsonSchema schema)
        {
            var json = (JsonValue)"test1";

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #19
0
        public void ValidateReturnsErrorOnValueOutOfRange(IJsonSchema schema)
        {
            var json = (JsonValue)"string";

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #20
0
        public void ValidateReturnsInvalicOnMultipleOf(IJsonSchema schema)
        {
            var json = (JsonValue)16;

            var results = schema.Validate(json);

            Assert.AreEqual(1, results.Errors.Count());
            Assert.AreEqual(false, results.Valid);
        }
Пример #21
0
        public void ValidateReturnsErrorOnInvalid(IJsonSchema schema)
        {
            var json = new JsonArray();

            var results = schema.Validate(json);

            Assert.AreEqual(1, results.Errors.Count());
            Assert.AreEqual(false, results.Valid);
        }
Пример #22
0
        public void ValidateReturnsValidOnCountLessThanMaxItems(IJsonSchema schema)
        {
            var json = new JsonArray {
                1, "string", false
            };

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #23
0
        public void ValidateReturnsValidOnUniqueItems(IJsonSchema schema)
        {
            var json = new JsonArray {
                1, "string", false, Math.PI, JsonValue.Null
            };

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #24
0
        public void ValidateReturnsValidOnCountGreaterThanMinItems(IJsonSchema schema)
        {
            var json = new JsonArray {
                1, "string", null, 4.0, "test", false
            };

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #25
0
        public void ValidateReturnsErrorOnTooFewItems(IJsonSchema schema)
        {
            var json = new JsonArray {
                1, "string"
            };

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #26
0
        public void ValidateReturnsErrorOnDuplicateItems(IJsonSchema schema)
        {
            var json = new JsonArray {
                1, "string", false, Math.PI, JsonValue.Null, 1
            };

            var results = schema.Validate(json);

            results.AssertInvalid();
        }
Пример #27
0
        public void ValidateReturnsValidOnValidItems(IJsonSchema schema)
        {
            var json = new JsonArray {
                "start", "string"
            };

            var results = schema.Validate(json);

            results.AssertValid();
        }
Пример #28
0
        public void ExamplesNotAnArray(IJsonSchema metaSchema)
        {
            var json = new JsonObject
            {
                ["examples"] = 5
            };

            var results = metaSchema.Validate(json);

            Assert.IsFalse(results.Valid);
        }
Пример #29
0
        public void Issue167_OneOfWithRequiredShouldFailValidation(IJsonSchema schema)
        {
            var json = new JsonObject
            {
                ["xyz"] = new JsonObject
                {
                    ["A"] = "abc"
                }
            };

            var results = schema.Validate(json);

            Assert.IsFalse(results.Valid);
            Console.WriteLine(string.Join("\n", results.Errors));
        }
        public override void OnActionExecuting(ActionExecutingContext context)
        {
            var maskText = context.ActionArguments.FirstOrDefault().Value?.ToString();

            if (string.IsNullOrEmpty(maskText))
            {
                _logger.LogWarning("Policy mask is missing or is not in a JSON format.");

                context.Result = new BadRequestObjectResult(new { error = "Policy mask is missing or is not in a JSON format." });
                return;
            }

            var result = _schema.Validate(JsonValue.Parse(maskText));

            if (!result.Valid)
            {
                var errors = result.Errors.Select(e => e.Message + " " + e.PropertyName);
                _logger.LogWarning("Errors during policy mask validation", errors);

                context.Result = new BadRequestObjectResult(new { error = errors });
            }
        }