Skip to content

Commit 520fd97

Browse files
committed
Include custom keyword validator in README.md
1 parent af0b4f5 commit 520fd97

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,41 @@ ExJsonSchema.Schema.resolve(%{"format" => "custom"}, custom_format_validator: fn
180180
181181
[format-spec]: https://json-schema.org/understanding-json-schema/reference/string.html#format
182182

183+
## Custom keywords
184+
185+
Keywords which are not part of the JSON Schema spec are ignored and not subjected to any validation by default.
186+
Should custom validation for extended keywords be required, you can provide a custom keyword validator which will be called with `(schema, property, data, path)` as parameters and is expected to return a list of `%Error{}` structs.
187+
188+
This validator can be configured globally:
189+
190+
```elixir
191+
config :ex_json_schema,
192+
:custom_keyword_validator,
193+
{MyKeywordValidator, :validate}
194+
```
195+
196+
Or by passing an option as either a `{module, function_name}` tuple or an anonymous function when resolving the schema:
197+
198+
```elixir
199+
ExJsonSchema.Schema.resolve(%{"x-my-keyword" => "value"}, custom_keyword_validator: {MyKeywordValidator, :validate})
200+
```
201+
202+
A partical example of how to use this functionality would be to extend a schema to support validating if strings contain a certain value via a custom keyword - `x-contains`. A simple implementation:
203+
204+
```elixir
205+
defmodule CustomValidator do
206+
def validate(_schema, {"x-contains", contains}, data, _path) do
207+
if not String.contains?(data, contains) do
208+
[%Error{error: "#{data} does not contain #{contains}"}]
209+
else
210+
[]
211+
end
212+
end
213+
214+
def validate(_, _, _, _), do: []
215+
end
216+
```
217+
183218
## License
184219

185220
Copyright (c) 2015 Jonas Schmidt

0 commit comments

Comments
 (0)