Sah::FAQ - Frequently asked questions
version 0.9.18
Schema language is a specialized language (DSL) that should be more concise to write than equivalent Perl code for common validation tasks. Its goal is never to be as powerful as Perl.
90% of the time, my schemas are some variations of the simple cases like:
"str*" ["str": {"len_between": [1, 10], "match": "some regex"}] ["str": {"in": ["a", "b", "c", ...]}] ["array": {"of": "some_other_type"}] ["hash": {"keys": {"key1": "some schema", ...}, "req_keys": [...], ...}]
and writing schemas is faster and less tedious/error-prone than writing equivalent Perl code, plus Data::Sah can generate JavaScript code and human description text for me. For more complex validation I stay with Sah until it starts to get unwieldy. It usually can go pretty far since I can add functions and custom clauses to its types; it's for the very complex and dynamic validation needs that I go pure Perl. Your mileage may vary.
Sah is an Indonesian word, meaning "valid" or "legal". It's picked because it's short.
The previous incarnation of this module uses the namespace Data::Schema, started in 2009 and deprecated in 2011 in favor of "Sah".
JSON schema limits its type system to that supported by JSON/JavaScript.
JSON schema's syntax is simpler.
It's metaschema (schema for the schema) is only about 130 lines. There are no shortcut forms.
JSON schema's features are more limited.
No expression, no function.
TBD
req
I am following SQL's behavior. A type declaration like:
INT
in SQL means NULL is allowed, while:
NULL
INT NOT NULL
means NULL is not allowed. The above is equivalent to specifying this in Sah:
int*
One could argue that setting req to 1 by default is safer/more convenient to her/whatever, and int should mean ["int", "req", 1] while something like perhaps int? means ["int", "req", 0]. But this is simply a design choice and each has its pros/cons. Nullable by default can also be convenient in some cases, like when specifying program options where most of the options are optional.
int
["int", "req", 1]
int?
["int", "req", 0]
default_req
Data::Sah
In general I am against compiler configuration which changes language behavior. In this case, it makes a simple schema like int to have ambiguous meaning (is undefined value allowed? Ir not allowed? It depends on compiler configuration).
not
not_in
There are generally no not_CLAUSE clauses. Instead, a generic !CLAUSE syntax is provided. Examples:
not_CLAUSE
!CLAUSE
// an integer that is not 0 ["int", {"!is": 0}] // a username that is not one of the forbidden/reserved ones ["str", {"!in": ["root", "admin", "superuser"]}]
in
!in
You can't do this since it will cause a conflict:
["str ", {"in": ["a","b","c"], "!in": ["x","y","z"]}]
However, you can do this:
["str ", {"cset&": [{"in": ["a","b","c"]}, {"!in": ["x","y","z"]}]}]
You can use if clause and negate the clauses. For example:
if
"if": [{"!div_by": 2}, {"!div_by": 5}]
Avoid any or all if you know that data is of a certain type
any
all
For performance and ease of reflection, it is better to create a custom clause than using the any type, especially with long list of alternatives. An example:
// dns_record is either a_record, mx_record, ns_record, cname_record, ... ["any", "of", [ "a_record", "mx_record", "ns_record", "cname_record", ... ] ] // base_record ["hash", "keys", { "owner": "str*", "ttl": "int", }] // a_record ["base_record", "merge.normal.keys", { "type": ["str*", "is", "A"], "address": "str*" }] // mx_record ["base_record", "merge.normal.keys", { "type": ["str*", "is", "MX"], "host": "str*", "prio": "int" }] ...
If you see the declaration above, every record is a hash. So it is better to declare dns_record as a hash instead of an any. But we need to select a different schema based on the type key. We can develop a custom clause like this:
dns_record
hash
type
["hash", "select_schema_on_key", ["type", { "A": "a_record", "MX": "mx_record", "NS": "ns_record", "CNAME": "cname_record", ... }]]
This will be faster.
If keys clause is specified, then by default only keys defined in keys clause is allowed, unless the .restrict attribute is set to false, in which case no restriction to allowed keys is done by the clause. The same case for re_keys.
keys
.restrict
re_keys
If allowed_keys and/or allowed_keys_re clause is specified, then only keys matching those clauses are allowed. This is in addition to restriction placed by other clauses, of course.
allowed_keys
allowed_keys_re
Set the .restrict attribute for keys or re_keys to false. Example:
["hash", { "keys": {"a": "int", "b": "int"}, "keys.restrict": 0, "allowed_keys": ["a", "b", "c", "d", "e"] ]
The above schema allows keys a, b, c, d, e and specifies values for a, b. Another example:
a, b, c, d, e
a, b
["hash", { "keys": {"a": "int", "b": "int"}, "keys.restrict": 0, "allowed_keys_re": "^[ab_]", ]
The above schema specifies values for a, b but still allows other keys beginning with an underscore.
req_keys
req_keys require keys to exist, but their values are governed by the schemas in keys or keys_re. Here are four combination possibilities, each with the schema:
keys_re
To require a hash key to exist, but its value can be undef:
["hash", "keys", {"a": "int"}, "req_keys": ["a"]]
To allow a hash key to not exist, but when it exists it must not be undef:
["hash", "keys", {"a": "int*"}]
To allow a hash key to not exist, or its value to be undef when exists:
["hash", "keys", {"a": "int"}]
To require hash key exist and its value must not be undef:
["hash", "keys", {"a": "int*"}, "req_keys": ["a"]]
XXX (Turn off hash merging using the '' Data::ModeMerge options key.
''
Steven Haryanto <stevenharyanto@gmail.com>
This software is copyright (c) 2012 by Steven Haryanto.
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:
You forgot a '=back' before '=head3'
To install Sah, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Sah
CPAN shell
perl -MCPAN -e shell install Sah
For more information on module installation, please visit the detailed CPAN module installation guide.