private static IJsonSchema _FromJson(JsonValue json, SchemaFactory schemaFactory, Uri documentPath = null) { if (json == null) { return(null); } IJsonSchema schema = null; switch (json.Type) { case JsonValueType.Object: var schemaDeclaration = json.Object.TryGetString("$schema"); var factory = _schemaFactories.FirstOrDefault(f => f.Id == schemaDeclaration); if (factory != null) { var id = json.Object.TryGetString(factory.IdKey); if (id == factory.MetaSchema.Id) { return(factory.MetaSchema); } schema = factory.Factory(); } schema = schema ?? schemaFactory.Factory(); if (json.Object.ContainsKey("$ref")) { schema = json.Object.Count > 1 ? new JsonSchemaReference(schema.GetType()) { Base = schema } } : new JsonSchemaReference(schema.GetType()); break; case JsonValueType.Array: schema = new JsonSchemaCollection(); break; case JsonValueType.Boolean: // not determining draft here is fine // True is intended to pass all instances // False is intended to fail all instances schema = new JsonSchema06(); break; default: throw new SchemaLoadException($"JSON Schema must be objects; actual type: {json.Type}"); } schema.DocumentPath = documentPath; schema.FromJson(json, null); return(schema); }
public SchemaValidationResults Validate(IJsonSchema schema, JsonValue json, JsonValue root) { var errors = new List <SchemaValidationError>(); var definitions = json.Object["definitions"]; if (definitions.Type != JsonValueType.Object) { errors.Add(new SchemaValidationError("definitions", "Property 'definitions' must contain an object.")); } var metaSchema = JsonSchemaFactory.GetMetaSchema(schema.GetType()); foreach (var value in definitions.Object.Values) { errors.AddRange(metaSchema.Validate(value).Errors); } return(new SchemaValidationResults(errors)); }
public SchemaValidationResults Validate(IJsonSchema schema, JsonValue json, JsonValue root) { var errors = new List <SchemaValidationError>(); var definitions = json.Object["definitions"]; if (definitions.Type != JsonValueType.Object) { var message = SchemaErrorMessages.Definitions.ResolveTokens(new Dictionary <string, object> { ["value"] = json }); errors.Add(new SchemaValidationError(schema, "definitions", message)); } var metaSchema = JsonSchemaFactory.GetMetaSchema(schema.GetType()); foreach (var value in definitions.Object.Values) { errors.AddRange(metaSchema.Validate(value).Errors); } return(new SchemaValidationResults(errors)); }
/// <summary> /// Creates a new instance of the <see cref="JsonSchemaReference"/> class that supports additional schema properties. /// </summary> /// <param name="reference">The relative (internal) or absolute (URI) path to the referenced type definition.</param> /// <param name="baseSchema">An instance of the base schema to use (either <see cref="JsonSchema04"/> or <see cref="JsonSchema06"/>).</param> /// <exception cref="ArgumentNullException">Thrown when <paramref name="reference"/> or <paramref name="baseSchema"/> is null.</exception> /// <exception cref="ArgumentException">Thrown when <paramref name="reference"/> is empty or whitespace or /// if <paramref name="baseSchema"/> is not of type <see cref="JsonSchema04"/> or <see cref="JsonSchema06"/>.</exception> public JsonSchemaReference(string reference, IJsonSchema baseSchema) : this(reference, baseSchema.GetType()) { Base = baseSchema; }