public void Hardcoded(IJsonSchema schema) { var json = schema.ToJson(null); var validation = schema.Validate(json); validation.AssertValid(); }
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); }
/// <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)); }
public void ValidateReturnsErrorOnNoneValid(IJsonSchema schema) { var json = new JsonObject(); var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsValidOnLessThanMaximum(IJsonSchema schema) { var json = (JsonValue)3; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsValidOnBoolean(IJsonSchema schema) { var json = (JsonValue)false; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsErrorOnMoreThanMaximum(IJsonSchema schema) { var json = (JsonValue)10; var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsValidOnMultipleOf_Negative(IJsonSchema schema) { var json = (JsonValue)(-15); var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsErrorOnLessThanMinimum(IJsonSchema schema) { var json = (JsonValue)4; var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsValidOnMoreThanMinimum(IJsonSchema schema) { var json = (JsonValue)10; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsErrorOnString(IJsonSchema schema) { JsonValue json = "string"; var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsErrorOnNonInteger(IJsonSchema schema) { var json = (JsonValue)1.2; var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsValidOnMultipleOf_Zero(IJsonSchema schema) { var json = (JsonValue)0; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsValidOnNull(IJsonSchema schema) { var json = JsonValue.Null; var results = schema.Validate(json); results.AssertValid(); }
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)); }
public void ValidateReturnsValidOnValueInRange(IJsonSchema schema) { var json = (JsonValue)"test1"; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsErrorOnValueOutOfRange(IJsonSchema schema) { var json = (JsonValue)"string"; var results = schema.Validate(json); results.AssertInvalid(); }
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); }
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); }
public void ValidateReturnsValidOnCountLessThanMaxItems(IJsonSchema schema) { var json = new JsonArray { 1, "string", false }; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsValidOnUniqueItems(IJsonSchema schema) { var json = new JsonArray { 1, "string", false, Math.PI, JsonValue.Null }; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsValidOnCountGreaterThanMinItems(IJsonSchema schema) { var json = new JsonArray { 1, "string", null, 4.0, "test", false }; var results = schema.Validate(json); results.AssertValid(); }
public void ValidateReturnsErrorOnTooFewItems(IJsonSchema schema) { var json = new JsonArray { 1, "string" }; var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsErrorOnDuplicateItems(IJsonSchema schema) { var json = new JsonArray { 1, "string", false, Math.PI, JsonValue.Null, 1 }; var results = schema.Validate(json); results.AssertInvalid(); }
public void ValidateReturnsValidOnValidItems(IJsonSchema schema) { var json = new JsonArray { "start", "string" }; var results = schema.Validate(json); results.AssertValid(); }
public void ExamplesNotAnArray(IJsonSchema metaSchema) { var json = new JsonObject { ["examples"] = 5 }; var results = metaSchema.Validate(json); Assert.IsFalse(results.Valid); }
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 }); } }