Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
    public static string ValidateSchema(string jsonText, string schemaText)
    {
        var serializer = new Manatee.Json.Serialization.JsonSerializer();
        var json       = Manatee.Json.JsonValue.Parse(jsonText);
        var schemaJson = Manatee.Json.JsonValue.Parse(schemaText);
        var schema     = new Manatee.Json.Schema.JsonSchema();

        schema.FromJson(schemaJson, serializer);
        Manatee.Json.Schema.JsonSchemaOptions.OutputFormat = Manatee.Json.Schema.SchemaValidationOutputFormat.Basic;


        var validationResults = schema.ValidateSchema();

        if (!validationResults.IsValid)
        {
            json = serializer.Serialize(validationResults);
            return(JsonHelper.FormatJson(json.ToString()));
        }
        else
        {
            return("");
        }
    }