@@ -113,45 +113,52 @@ use Maml\Schema\S;
113113
114114$schema = S::object([
115115 'host' => S::string(),
116- 'port' => S::integer(),
117- 'tags' => S::arrayOf(S::string()),
116+ 'port' => S::integer(min: 1, max: 65535 ),
117+ 'tags' => S::arrayOf(S::string(), minItems: 1 ),
118118 'ssl' => S::optional(S::boolean()),
119119 'mode' => S::enum('fast', 'safe', 'auto'),
120+ 'version' => S::optional(S::string(pattern: '/^\d+\.\d+\.\d+$/')),
120121]);
121122
122123$doc = Maml::parseAst($source);
123124$errors = Maml::validate($doc, $schema);
124125
125126foreach ($errors as $error) {
126- // $error->message "Missing required property "host" "
127- // $error->path "$.host "
128- // $error->position Position(line: 1, column: 1 )
129- echo Maml::errorSnippet($source, $error->position , $error->message);
127+ // $error->message "Expected integer, got string "
128+ // $error->path "$.port "
129+ // $error->span Span(start: Position(line: 3, ...), end: ... )
130+ echo Maml::errorSnippet($source, $error->span , $error->message);
130131}
131132```
132133
133134### Available schema types
134135
135- | Builder | Matches |
136- | ---------------------------------| ----------------------------------------------------|
137- | ` S::string() ` | String or raw string |
138- | ` S::integer() ` | Integer |
139- | ` S::float() ` | Float |
140- | ` S::number() ` | Integer or float |
141- | ` S::boolean() ` | Boolean |
142- | ` S::null() ` | Null |
143- | ` S::any() ` | Anything |
144- | ` S::literal('x') ` | Exact value |
145- | ` S::enum('a', 'b') ` | One of the listed values |
146- | ` S::object([...]) ` | Object with typed properties, rejects unknown keys |
147- | ` S::object([...], S::any()) ` | Same, but allows extra keys |
148- | ` S::object([...], S::string()) ` | Same, extra keys must match schema |
149- | ` S::orderedObject([...]) ` | Object with properties in order |
150- | ` S::map(schema) ` | Object with any keys, typed values |
151- | ` S::optional(schema) ` | Property may be absent |
152- | ` S::arrayOf(schema) ` | Array of uniform type |
153- | ` S::tuple([s1, s2]) ` | Fixed-length array |
154- | ` S::union(s1, s2) ` | One of several schemas |
136+ | Builder | Matches |
137+ | ------------------------------------| ----------------------------------------------------|
138+ | ` S::string() ` | String or raw string |
139+ | ` S::string(pattern: '/.../') ` | String matching regex |
140+ | ` S::integer() ` | Integer |
141+ | ` S::integer(min: 0, max: 100) ` | Integer within range |
142+ | ` S::float() ` | Float |
143+ | ` S::float(min: 0.0, max: 1.0) ` | Float within range |
144+ | ` S::number() ` | Integer or float |
145+ | ` S::number(min: 0) ` | Number with minimum |
146+ | ` S::boolean() ` | Boolean |
147+ | ` S::null() ` | Null |
148+ | ` S::any() ` | Anything |
149+ | ` S::literal('x') ` | Exact value |
150+ | ` S::enum('a', 'b') ` | One of the listed values |
151+ | ` S::object([...]) ` | Object with typed properties, rejects unknown keys |
152+ | ` S::object([...], S::any()) ` | Same, but allows extra keys |
153+ | ` S::object([...], S::string()) ` | Same, extra keys must match schema |
154+ | ` S::orderedObject([...]) ` | Object with properties in order |
155+ | ` S::map(schema) ` | Object with any keys, typed values |
156+ | ` S::optional(schema) ` | Property may be absent |
157+ | ` S::arrayOf(schema) ` | Array of uniform type |
158+ | ` S::arrayOf(schema, minItems: 1) ` | Array with minimum length |
159+ | ` S::arrayOf(schema, maxItems: 10) ` | Array with maximum length |
160+ | ` S::tuple([s1, s2]) ` | Fixed-length array |
161+ | ` S::union(s1, s2) ` | One of several schemas |
155162
156163## License
157164
0 commit comments