json_schema 1.0.10

  • 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.

Use this package as a library

1. Depend on it

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


dependencies:
  json_schema: ^1.0.10

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 flutter 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.10 Jul 26, 2018 Go to the documentation of json_schema 1.0.10 Download json_schema 1.0.10 archive
1.0.9 Jul 2, 2018 Go to the documentation of json_schema 1.0.9 Download json_schema 1.0.9 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 Go to the documentation of json_schema 1.0.1 Download json_schema 1.0.1 archive

All 15 versions...

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

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

  • Dart: 2.0.0
  • pana: 0.12.3

Platforms

Detected platforms: Flutter, other

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

Suggestions

Format bin/gensamples.dart.

Run dartfmt to format bin/gensamples.dart.

Fix bin/schemadot.dart.

Analysis of bin/schemadot.dart reported 4 hints:

line 92 col 61: 'allowMultiple' is deprecated and shouldn't be used.

line 93 col 63: 'allowMultiple' is deprecated and shouldn't be used.

line 102 col 9: 'allowMultiple' is deprecated and shouldn't be used.

line 142 col 7: The top level variable '_logger' isn't used.

Fix lib/json_schema.dart.

Analysis of lib/json_schema.dart reported 1 hint:

line 77 col 11: Always override hashCode if overriding ==.

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.

Fix additional 3 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/schema_dot.dart (Run dartfmt to format lib/schema_dot.dart.)
  • lib/src/json_schema/schema.dart (Run dartfmt to format lib/src/json_schema/schema.dart.)
  • lib/src/json_schema/validator.dart (Run dartfmt to format lib/src/json_schema/validator.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.2 <3.0.0
args >=0.11.0 <2.0.0 1.5.0
dart2_constant ^1.0.0 1.0.2+dart2
logging >=0.9.3 <0.12.0 0.11.3+2
path ^1.3.0 1.6.2
Dev dependencies
coverage >=0.7.6
dart_dev any
dart_style any
dependency_validator ^1.0.0
test ^0.12.13