JSON::Schema::Shorthand - Alternative, condensed format for JSON Schemas
version 0.0.2
use JSON::Schema::Shorthand; my $schema = js_shorthand({ object => { foo => 'number', bar => { type => 'string', required => 1 } }); # $schema is # { # type => 'object', # properties => { # foo => { type => 'number' }, # bar => { type => string }, # } # required => [ 'bar' ], # }
JSON Schema is a useful beast, but its schema definition can be a little bit more long-winded than necessary. This module allows to use a few shortcuts that will be expanded into their canonical form.
CAVEAT: the module is still very young, and there are plenty of properties this module should expand and does not. So don't trust it blindly. If you hit such a case, raise a ticket and I'll refine the process.
my $schema = js_shorthand $shorthand;
The module exports a single function, js_shorthand, that takes in a JSON schema in shorthand notation and returns the expanded, canonical schema form.
js_shorthand
If you don't like the name js_shorthand, you can always import it under a different name in your namespace.
use JSON::Schema::Shorthand 'js_shorthand' => { -as => 'expand_json_schema' }; ...; my $schema = expand_json_schema $shorthand;
If a string type is encountered where a property definition is expected, the string is expanded to the object { "type": type }.
type
{ "type": type }
{ "foo": "number", "bar": "string" }
expands to
{ "foo": { "type": "number" }, "bar": { "type": "string" } }
If the string begins with a #, the type is assumed to be a reference and #type is expanded to { "$ref": type }.
#
#type
{ "$ref": type }
{ "foo": "#/definitions/bar" }
becomes
{ "foo": { "$ref": "#/definitions/bar" } }
object
{ object: properties } expands to { type: "object", properties }.
{ object: properties }
{ type: "object", properties }
shorthand expanded ------------------------ --------------------------- foo: { foo: { object: { type: "object", bar: { } properties: { } bar: { } } } }
array
{ array: items } expands to { type: "array", items }.
{ array: items }
{ type: "array", items }
shorthand expanded ------------------------ --------------------------- foo: { foo: { array: 'number' type: "array", } items: { type: 'number' } }
required
If the required attribute is set to true for a property, it is bubbled up to the required attribute of its parent object.
true
shorthand expanded ------------------------ --------------------------- foo: { foo: { properties: { required: [ 'bar' ], bar: { required: true }, properties: { baz: { } bar: {}, } baz: {} } }
* JSON Schema specs - http://json-schema.org/
* JavaScript version of this module - http://github.com/yanick/json-shema-shorthand
Yanick Champoux <yanick@babyl.dyndns.org>
This software is copyright (c) 2016 by Yanick Champoux.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
1 POD Error
The following errors were encountered while parsing the POD:
Unknown directive: =head2Shorthands
To install JSON::Schema::Shorthand, copy and paste the appropriate command in to your terminal.
cpanm
cpanm JSON::Schema::Shorthand
CPAN shell
perl -MCPAN -e shell install JSON::Schema::Shorthand
For more information on module installation, please visit the detailed CPAN module installation guide.