Skip to content

Instantly share code, notes, and snippets.

@mrenvoize
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrenvoize/e8a5574429cc7f7211b5 to your computer and use it in GitHub Desktop.
Save mrenvoize/e8a5574429cc7f7211b5 to your computer and use it in GitHub Desktop.
SchemaValidator following $ref
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"published": {
"type": "boolean"
},
"embeded": {
"$ref": "#/definitions/embeded"
}
},
"additionalProperties": false,
"required": [
"published",
"embeded"
],
"definitions": {
"embeded": {
"type": "object",
"properties": {
"prop1": {
"type": "string"
},
"prop2": {
"type": "integer"
}
},
"required": [
"prop1",
"prop2"
]
}
}
}
{
"published": false,
"embeded": {
"prop1": "string",
"prop2": 2
}
}
#!/usr/bin/env perl
use strict;
use warnings;
use FindBin;
BEGIN { unshift @INC, "$FindBin::Bin/../lib" }
use Data::Printer colored => 1;
use JSON;
use Swagger2::SchemaValidator;
use File::Slurp;
my $validator = Swagger2::SchemaValidator->new;
my $schema = read_file( "list-schema.json" ) or die "cannot open < list-schema.json: $!";
my $schema_ref = from_json( $schema );
my $list = read_file( "list.json") or die "cannot open < list.json: $!";
my $list_json = from_json($list);
my @errors = $validator->validate($list_json, $schema_ref);
print "Error: " . p( @errors );
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment