NAME
Sah::Examples - Example Sah schemas
VERSION
This document describes version 0.05 of Sah::Examples (from Perl distribution Sah-Examples), released on 2017-05-22.
DESCRIPTION
This distribution contains various examples of Sah schemas in its per-dist share dir.
This POD also contains examples of schemas.
EXAMPLES
The examples are written in JSON with Javascript-style comments (// comment
).
Various simple examples
// integer, optional
"int"
// required integer
"int*"
// same thing
[
"int"
, {
"req"
: 1}]
// integer between 1 and 10
[
"int*"
, {
"min"
: 1,
"max"
: 10}]
// same thing, the curly brace is optional (
unless
for
advanced stuff)
[
"int*"
,
"min"
, 1,
"max"
, 10]
// array of integers between 1 and 10
[
"array*"
, {
"of"
: [
'int*'
,
"between"
: [1, 10]]}]
// a byte (let
's assign it to a new type '
byte')
[
"int"
, {
"between"
: [0,255]}]
// a byte that's divisible by 3
[
"byte"
, {
"div_by"
: 3}]
// a byte that's divisible by 3
*and
* 5
[
"byte"
, {'div_by&": [3, 5]}]
// a byte that's divisible by 3
*or
* 5
[
"byte"
, {
"div_by|"
: [3, 5]}]
// a byte that's
*in
*divisible
by 3
[
"byte"
, {
"!div_by"
: 3}]
Type: array
// minimum number of elements
[
"array*"
, {
"min_len"
: 1}]
// maximum number of elements
[
"array*"
, {
"max_len"
: 10}]
Type: hash
// hash must only contain
keys
a, b, or c
// Valid: {}, {
"a"
: 1}, {
"a"
: 1,
"b"
:
"x"
,
"c"
: [1]}
// Invalid: {
"d"
: 1} (unknown key), {
"a"
: 1.1} (key
'a'
is not
int
)
[
"hash"
, {
"keys"
: {
"a"
:
"int"
,
"b"
:
"str*"
,
"c"
: [
"array"
,
"min_len"
, 1]}}]
// hash can contain a, b, c or other
keys
// Valid: {
"d"
: 1}, {
"d"
: null}
// Invalid: {
"a"
: 1.1} (key
'a'
is not
int
)
[
"hash"
, {
"keys"
: {
"a"
:
"int"
,
"b"
:
"str*"
,
"c"
: [
"array"
,
"min_len"
, 1]},
"keys.restrict"
: 0}]
// hash must contain
keys
a, b; can contain also c but
no
other
keys
[
"hash"
, {
"req_keys"
: [
"a"
,
"b"
],
"keys"
: {
"a"
:
"int"
,
"b"
:
"str*"
,
"c"
: [
"array"
,
"min_len"
, 1]}}]
// hash must contain
keys
a, b; can contain c or other
keys
[
"hash"
, {
"req_keys"
: [
"a"
,
"b"
],
"keys"
: {
"a"
:
"int"
,
"b"
:
"str*"
,
"c"
: [
"array"
,
"min_len"
, 1]},
"keys.restrict"
: 0}]
Clause attribute
Coercion rule
Explicitly enable rule(s) that is (are) not enabled by default:
// allow input as comma-separated string, e.g.
"1,20,3,4"
[
"array"
, {
"of"
:
"int"
,
"x.perl.coerce_rules"
: [
"str_comma_sep"
]}]
Explicitly disable rule(s) that is (are) enabled by default:
// don't allow duration to be coerced from integer (number of seconds)
[
"duration"
, {
"x.perl.coerce_rules"
: [
"!float_secs"
]}]
Expression
Function
Merging
// an address hash (let
's assign it to a new type called '
address')
[
"hash"
, {
// recognized
keys
"keys"
: {
"line1"
: [
"str*"
, {
"max_len"
: 80}],
"line2"
: [
"str*"
, {
"max_len"
: 80}],
"city"
: [
"str*"
, {
"max_len"
: 60}],
"province"
: [
"str*"
, {
"max_len"
: 60}],
"postcode"
: [
"str*"
, {
"len_between"
: [4, 15],
"match"
:
"^[\w-]{4,15}$"
}],
"country"
: [
"str*"
, {
"len"
: 2,
"match"
:
"^[A-Z][A-Z]$"
}]
},
//
keys
that must exist in data
"req_keys"
: [
"line1"
,
"city"
,
"province"
,
"postcode"
,
"country"
]
]
// a US address, let
's base it on '
address
' but change '
postcode' to
[
"address"
, {
"merge.subtract.keys"
: {
"postcode"
: null},
"merge.normal.keys"
: {
"zipcode"
: [
"str*"
,
"len"
, 5,
"match"
,
"^\d{5}$"
],
"country"
: [
"str*"
,
"is"
,
"US"
]
},
"merge.subtract.req_keys"
: [
"postcode"
],
"merge.add.req_keys"
: [
"zipcode"
]
]
HOMEPAGE
Please visit the project's homepage at https://metacpan.org/release/Sah-Examples.
SOURCE
Source repository is at https://github.com/perlancar/perl-Sah-Examples.
BUGS
Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Sah-Examples
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
SEE ALSO
Sah specification, which contains a spectest in its per-dist share dir.
Data::Sah, Perl implementation for Sah.
Data::Sah::Coerce for more information about coercion.
Various Sah::Schema::*
modules (in Sah::Schema::*
or Sah::Schemas::*
distributions) which contain schemas in Perl modules.
AUTHOR
perlancar <perlancar@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2017, 2015 by perlancar@cpan.org.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.