API Documentation
validate
The validate
function checks a schema against JSON (FSharp.Data.JsonValue) returning
a ValidationResult
.
1:
|
|
A ValidationResult
is simply defined as
1: 2: 3: |
|
See the tutorial for a full example.
Below are the many ways to define JsonSchema
s.
Anything
A JsonSchema
that validates against anything
Useful for those times when you just don't care, or no actual structure is expected.
1:
|
|
Exact Matches
Match exactly a JsonValue
1:
|
|
example: schema to match exactly 42
1:
|
|
Match exactly one of a few possible JsonValue
s
1:
|
|
example: schema to match exactly null
or "the answer"
1:
|
|
Literals
Numbers
Validate that JSON is any number
1:
|
|
Strings
Validate that JSON is any string
1:
|
|
Validate a string with specific criteria
1:
|
|
Where StringAttributes
can be any combination of
IsNotEmpty
- ensure the string has length-
MeetsCriteria of string*(string -> bool)
- ensure the string meets an arbitrary condition wherestring
is a description of the criteria, useful for error messages(string -> bool)
is the actual predicate (function returning bool) to check
example: schema to match "long" strings that contain at least one dash
1:
|
|
Objects
Validate an object with specific keys
1:
|
|
Where KeyValidation*JsonSchema
specifies key-value schemas such that
KeyValidation
indicates required or optional (omittable) keys andJsonSchema
constrains the allowable value under the keys
Note that ( .= )
and ( .?= )
are super useful for defining object schemas where
( .= )
indicates a required key whose value must abide the given schema and-
( .?= )
indicates an optional key (can be left out) whose value, if the key is given, must abide the given schema and
example: schemas to match addresses and people who may or may not have an address
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: |
|
Arrays
Validate an array
1:
|
|
Where ArrayAttributes
can be any combination of
LengthIsAtLeast of int
constrains the minimum length of the arrayLengthIsAtMost of int
constrains the maximum length of the array-
LengthIsExactly of int
constrains the exact length of the array (probably shouldn't be used with the two above) ItemsMatch of JsonSchema
specifies the schema of the array's items
example: schema to match an array of exactly 42 numbers
1:
|
|
Alternatives
Given multiple schemas, validate that any match
1:
|
|
Where JsonSchema
can be any other JsonSchema
described hereabouts
example: schema to match either a string, number or null
1:
|
|
Laziness
Match a schema, lazily
Useful when your JSON types may contain themselves
1:
|
|
Where unit -> JsonSchema
is a function that just returns another
JsonSchema
to validate.
example: schema to match a memory that may link to a memory that may link to a memory that may lin...
1: 2: 3: 4: 5: 6: |
|