Dancer::Plugin::JSON::Schema - JSON::Schema interface for Dancer applications
version 0.001
use Dancer; use Dancer::Plugin::JSON::Schema qw(json_schema); post '/search' => sub { my $structure = param('q'); my $result = json_schema('default')->validate($structure); # If you are accessing the 'default' schema, then you can just do: my $result = json_schema->validate($structure); ... }; dance;
This plugin makes it very easy to create Dancer applications that interface with JSON Schema.
It automatically exports the keyword json_schema which returns an JSON::Schema object.
json_schema
You just need to configure where to get the schema from.
For performance, JSON::Schema objects are cached in memory and are lazy loaded the first time they are accessed.
Configuration can be done in your Dancer config file.
Here is a simple example. It defines one schema named default:
default
plugins: 'JSON::Schema': default: schema: schemas/item.json
In this example, there are 2 schemas configured named default and accessories:
accessories
plugins: 'JSON::Schema': default: schema: schemas/item.json user: schema: schemas/user.json
Each schema configured must at least have a schema option set.
schema
If you only have one schema configured, or one of them is named default, you can call json_schema without an argument to get the only or default schema, respectively.
Alternatively, you may also declare your schema information inside a hash named schema_info:
schema_info
plugins: 'JSON::Schema': default: schema_info: schema: schemas/item.json
Aliases allow you to reference the same underlying schema with multiple names.
For example:
plugins: 'JSON::Schema': default: schemas/item.json products: alias: default
Now you can access the default schema with json_schema(), json_schema('default'), or json_schema('products').
json_schema()
json_schema('default')
json_schema('products')
my $result = json_schema->validate( $structure );
The json_schema keyword returns a JSON::Schema object ready for you to use.
If you have configured only one schema, then you can simply call json_schema with no arguments.
If you have configured multiple schemas, you can still call json_schema with no arguments if there is a schema named default in the configuration.
With no argument, the default schema is returned.
Otherwise, you must provide json_schema() with the name of the schema:
my $user = json_schema('accessories')->select( ... );
This module is based on Dancer::Plugin::DBIC, as at 22 October 2014, and adapted for JSON::Schema by Daniel Perrett.
The following had authored Dancer::Plugin::DBIC at this time:
Al Newkirk <awncorp@cpan.org>
Naveed Massjouni <naveed@vt.edu>
The following had made contributions to Dancer::Plugin::DBIC at this time:
Alexis Sukrieh <sukria@sukria.net>
Dagfinn Ilmari Mannsåker <https://github.com/ilmari>
David Precious <davidp@preshweb.co.uk>
Fabrice Gabolde <https://github.com/fgabolde>
Franck Cuny <franck@lumberjaph.net>
Steven Humphrey <https://github.com/shumphrey>
Yanick Champoux <https://github.com/yanick>
Daniel Perrett <dp13@sanger.ac.uk>
This software is copyright (c) 2010 by awncorp.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install Dancer::Plugin::JSON::Schema, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Dancer::Plugin::JSON::Schema
CPAN shell
perl -MCPAN -e shell install Dancer::Plugin::JSON::Schema
For more information on module installation, please visit the detailed CPAN module installation guide.