The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

JSON::Schema::Shorthand - Alternative, condensed format for JSON Schemas

VERSION

version 0.0.2

SYNOPSIS

    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' ],
    # }

DESCRIPTION

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.

js_shorthand

    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.

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;

Types as string

If a string type is encountered where a property definition is expected, the string is expanded to the object { "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 }.

    { "foo": "#/definitions/bar" } 

becomes

    { "foo": { "$ref": "#/definitions/bar" } }

object property

{ object: properties } expands to { type: "object", properties }.

    shorthand                              expanded
    ------------------------               ---------------------------
    foo: {                                  foo: {
        object: {                               type: "object",
            bar: { }                            properties: {
        }                                           bar: { }
    }                                           }
                                            }

array property

{ array: items } expands to { type: "array", items }.

    shorthand                              expanded
    ------------------------               ---------------------------
    foo: {                                  foo: {
        array: 'number'                         type: "array",
    }                                           items: {
                                                    type: 'number' 
                                                }
                                            }

required property

If the required attribute is set to true for a property, it is bubbled up to the required attribute of its parent object.

    shorthand                              expanded
    ------------------------               ---------------------------

    foo: {                                  foo: {
        properties: {                           required: [ 'bar' ],
          bar: { required: true },              properties: { 
          baz: { }                                bar: {},
        }                                         baz: {}
    }                                       }

SEE ALSO

* JSON Schema specs - http://json-schema.org/

* JavaScript version of this module - http://github.com/yanick/json-shema-shorthand

AUTHOR

Yanick Champoux <yanick@babyl.dyndns.org>

COPYRIGHT AND LICENSE

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:

Around line 136:

Unknown directive: =head2Shorthands