/// <summary> /// Downloads and registers a schema at the specified URI. /// </summary> public static JsonSchema Get(string uri) { JsonSchema schema; lock (_schemaLookup) { uri = uri.TrimEnd('#'); if (!_schemaLookup.TryGetValue(uri, out schema)) { var schemaJson = JsonSchemaOptions.Download(uri); var schemaValue = JsonValue.Parse(schemaJson); schema = new JsonSchema { DocumentPath = new Uri(uri, UriKind.RelativeOrAbsolute) }; schema.FromJson(schemaValue, _serializer); var structureErrors = schema.ValidateSchema(); if (!structureErrors.IsValid) { throw new SchemaLoadException("The given path does not contain a valid schema.", structureErrors); } _schemaLookup[uri] = schema; } } return(schema); }
/// <summary> /// Downloads and registers a schema at the specified URI. /// </summary> public static IJsonSchema Get(string uri) { IJsonSchema schema; lock (_schemaLookup) { uri = uri.TrimEnd('#'); if (!_schemaLookup.TryGetValue(uri, out schema)) { var schemaJson = JsonSchemaOptions.Download(uri); var schemaValue = JsonValue.Parse(schemaJson); schema = JsonSchemaFactory.FromJson(schemaValue, new Uri(uri)); var metaSchemas = new IJsonSchema[] { JsonSchema07.MetaSchema, JsonSchema06.MetaSchema, JsonSchema04.MetaSchema }; SchemaValidationResults validation = null; if (schema.Schema != null) { var bySchema = metaSchemas.FirstOrDefault(s => s.Id == schema.Schema); if (bySchema != null) { validation = bySchema.Validate(schemaValue); } } else { foreach (var metaSchema in metaSchemas) { validation = metaSchema.Validate(schemaValue); if (validation.Valid) { break; } } } if (validation != null && !validation.Valid) { var errors = string.Join(Environment.NewLine, validation.Errors.Select(e => e.Message)); throw new ArgumentException($"The given path does not contain a valid schema. Errors: \n{errors}"); } _schemaLookup[uri] = schema; } } return(schema); }