json_schema 1.0.8

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 96

Json Schema

A dart:io dependent library for validating json instances against json schema (version Draft 04)

Build Status

How To Validate

To validate instances against a schema first create the schema, then call validate on it with an json instance. This can be done with an url:

Example 1

String url = "http://json-schema.org/draft-04/schema";
Schema.createSchemaFromUrl(url)
  .then((schema) {
    print('Does schema validate itself? ${schema.validate(schema.schemaMap)}');
  });

In this example a schema is created from the url and its stored contents are validated against itself. Since the referenced schema is the schema for schemas and the instance is, of course, a schema, the result prints true.

Example 2

An url can point to a local file, either of format file:///absolute_path_to/schema.json or subfolder/schema.json where subfolder is a subfolder of current working directory. An example of this can be found in example/from_url/validate_instance_from_url.dart

  url = "grades_schema.json";
  Schema.createSchemaFromUrl(url)
    .then((schema) {
      var grades = JSON.parse('''
{
    "semesters": [
        {
            "semester": 1,
            "grades": [
                {
                    "type": "homework",
                    "date": "09/27/2013",
                    "grade": 100,
                    "avg": 93,
                    "std": 8
                },
                {
                    "type": "homework",
                    "date": "09/28/2013",
                    "grade": 100,
                    "avg": 60,
                    "std": 25
                }
            ]  
        }
      ]
}''');
      
      print('''Does grades schema validate $grades
  ${schema.validate(grades)}''');

In this example the schema is read from file grades_schema.json in the current directory and a valid instance is submitted for validation (in the string of the print statement). This example also prints true.

Example 3

A schema can be created with a Map that is either hand-crafted or the result of a call to json parse.

  //////////////////////////////////////////////////////////////////////
  // Define schema in code
  //////////////////////////////////////////////////////////////////////
  var mustBeIntegerSchema = {
    "type" : "integer"
  };

  var n = 3;
  var decimals = 3.14;
  var str = 'hi';

  Schema.createSchema(mustBeIntegerSchema)
    .then((schema) {
      print('$n => ${schema.validate(n)}');
      print('$decimals => ${schema.validate(decimals)}');
      print('$str => ${schema.validate(str)}');
    });

This example creates a schema requiring the type be integer. It then tests against three instances with the following results:

3 => true
3.14 => false
hi => false

How To Use Schema Information

Schema information can be used for validation; but it can also be a valuable source of information about the structure of data. The Schema class provided here works by fully parsing the schema first, which itself must be valid on all paths within the schema. The only invalid content of a provided schema are free-form properties containing schema that are not referenced. Accessors are provided for the meta-data associated with a schema, so tools can do stuff with it.

One example use is the schemadot program included in the bin folder which takes schema as input and outputs a Graphviz dot file, providing a picture of the schema. This does not provide all information of the schema, and is a work in progress - but it can be useful to see what a schema is.

For example, the grades_schema.json is:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title" : "Grade Tracker",
    "type" : "object",
    "additionalProperties" : false,
    "properties" : {
	"semesters" : {
	    "type" : "array",
	    "items" : {
                "type" : "object",
                "additionalProperties" : false,
                "properties" : {
                    "semester": { "type" : "integer" },
                    "grades" : {
                        "type" : "array",
                        "items" : {
                            "type" : "object",
                            "additionalProperties" : false,
                            "required" : [ "date", "type", "grade", "std" ],
                            "properties" : {
                                "date" : { "type" : "string"},
                                "type" : { "enum" : [ "homework", "quiz", "test", "final_exam" ] },
                                "grade" : { "type" : "number"},
                                "std" : { 
                                    "oneOf" : [ 
                                        {"type" : "number"}, 
                                        {"type" : "null"}
                                    ] 
                                },
                                "avg" : { 
                                    "oneOf" : [ 
                                        {"type" : "number"}, 
                                        {"type" : "null"}
                                    ] 
                                }
                            }
                        }
                    }
                }
            }
  	    }
    }
}

And the generated image is:

Grades!

For more detailed image open link: <a href="https://raw.github.com/patefacio/json_schema/master/example/from_url/grades_schema.png" target="_blank">Grade example schema diagram</a>

TODOS

  • Add a remote ref test that does not require files vended from local host
  • Add support for optional tests: format

1.0.8

  • Code cleanup
  • Strong mode
  • Switch build tools to dart_dev

1.0.7

  • Update dependency constraint on the args package.

1.0.3

  • Add a dependency on the args package.

1.0.2

  • Add a dependency on the logging package.

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_schema: "^1.0.8"

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_schema/json_schema.dart';
        
Version Uploaded Documentation Archive
1.0.8 Jan 18, 2018 Go to the documentation of json_schema 1.0.8 Download json_schema 1.0.8 archive
1.0.7 Dec 20, 2017 Go to the documentation of json_schema 1.0.7 Download json_schema 1.0.7 archive
1.0.6 Nov 16, 2016 Go to the documentation of json_schema 1.0.6 Download json_schema 1.0.6 archive
1.0.5 Nov 15, 2016 Go to the documentation of json_schema 1.0.5 Download json_schema 1.0.5 archive
1.0.4 Jun 9, 2016 Go to the documentation of json_schema 1.0.4 Download json_schema 1.0.4 archive
1.0.3 Dec 1, 2015 Go to the documentation of json_schema 1.0.3 Download json_schema 1.0.3 archive
1.0.2 Nov 29, 2015 Go to the documentation of json_schema 1.0.2 Download json_schema 1.0.2 archive
1.0.1 May 25, 2015 failed Download json_schema 1.0.1 archive
1.0.0 Nov 8, 2013 Go to the documentation of json_schema 1.0.0 Download json_schema 1.0.0 archive
0.0.2 Sep 16, 2013 Go to the documentation of json_schema 0.0.2 Download json_schema 0.0.2 archive

All 11 versions...

Analysis

We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
92 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
96
Learn more about scoring.

Platforms

Detected platforms: Flutter, other

Primary library: package:json_schema/json_schema.dart with components: io.

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 7 hints.

    Run dartfmt to format lib/json_schema.dart.

    Run dartfmt to format lib/schema_dot.dart.

    Similar analysis of the following files failed:

    • lib/src/json_schema/schema.dart (hint)
    • lib/src/json_schema/validator.dart (hint)
  • Maintain an example.

    None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use json_schema.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.2 <2.0.0
args >=0.11.0 <2.0.0 1.4.2
logging >=0.9.3 <0.12.0 0.11.3+1
path ^1.3.0 1.5.1
Dev dependencies
coverage >=0.7.6
dart_dev any
dart_style any
dependency_validator ^1.0.0
test ^0.12.13