Safe Haskell | None |
---|---|
Language | Haskell2010 |
Data.Aeson.Validation
Contents
Description
JSON schema validation.
- data Schema
- schema :: Schema -> Value -> Bool
- validate :: Schema -> Value -> [Text]
- bool :: Schema
- true :: Schema
- false :: Schema
- number :: Schema
- theNumber :: Scientific -> Schema
- someNumber :: A schema => (Scientific -> Bool) -> schema
- integer :: Schema
- theInteger :: Integer -> Schema
- someInteger :: A schema => (Integer -> Bool) -> schema
- string :: Schema
- theString :: Text -> Schema
- someString :: A schema => (Text -> Bool) -> schema
- regex :: Text -> Schema
- datetime :: Schema
- data Field
- data Path
- object :: [Field] -> Schema
- object' :: [Field] -> Schema
- (.:) :: Path -> Schema -> Field
- (.:?) :: Path -> Schema -> Field
- array :: Schema -> Schema
- sizedArray :: Int -> Int -> Schema -> Schema
- set :: Schema -> Schema
- sizedSet :: Int -> Int -> Schema -> Schema
- tuple :: [Schema] -> Schema
- anything :: Schema
- nullable :: Schema -> Schema
Schema validation
An opaque JSON Schema
.
Instances
Fractional Schema Source # | The
|
Num Schema Source # | The
|
IsString Schema Source # |
Examples:
|
Semigroup Schema Source # | The
For Examples:
|
Boolean schemas
Number schemas
theNumber :: Scientific -> Schema Source #
An approximate Number
, with a relative tolerance of
1^-9
(the smaller number must be within 0.0000001%
of the larger number).
You may use a floating point literal instead.
Here is how you'd implement theNumber
using someNumber
, in case you want
to use a different tolerance:
theNumber
::Scientific
->Schema
theNumber
n =someNumber
(isClose n) where isClose ::Scientific
->Scientific
->Bool
isClose a b =abs
(a-b)<=
1e-9 *max
(abs
a) (abs
b)
Examples
>>>
schema (theNumber 1.5) (Number 1.5)
True
>>>
schema 2.5 (Number 2.500000001)
True
someNumber :: A schema => (Scientific -> Bool) -> schema Source #
Some Number
.
The unexported A
typeclass exists to overload someNumber
with precicely
two possible types:
someNumber
:: (Scientific
->Bool
) ->Schema
someNumber
:: (Scientific
->Bool
) ->Text
->Schema
The optional Text
argument is used for error reporting if validation
fails.
Examples
>>>
schema (someNumber (> 5)) (Number 6)
True
>>>
validate (someNumber (> 5) "greater than 5") (Number 4)
["failed predicate: greater than 5"]
>>>
validate (someNumber (> 5)) (Number 4)
["failed predicate"]
Any integer Number
.
Examples
>>>
schema integer (Number 1.0)
True
>>>
schema integer (Number 1.5)
False
theInteger :: Integer -> Schema Source #
An exact integer Number
.
You may use an integer literal instead.
Examples
>>>
schema (theInteger 1) (Number 1)
True
>>>
schema 1 (Number 2)
False
someInteger :: A schema => (Integer -> Bool) -> schema Source #
Some integer Number
.
The unexported A
typeclass exists to overload someInteger
with
precicely two possible types:
someInteger
:: (Integer
->Bool
) ->Schema
someInteger
:: (Integer
->Bool
) ->Text
->Schema
The optional Text
argument is used for error reporting if validation
fails.
Examples
>>>
schema (someInteger (> 5)) (Number 6.0)
True
>>>
validate (someInteger (> 5) "greater than 5") (Number 6.5)
["failed predicate: greater than 5"]
>>>
validate (someInteger (> 5)) (Number 6.5)
["failed predicate"]
String schemas
theString :: Text -> Schema Source #
An exact String
.
You may use a string literal instead (requires -XOverloadedStrings
).
Examples
>>>
schema (theString "foo") (String "foo")
True
>>>
schema "foo" (String "bar")
False
someString :: A schema => (Text -> Bool) -> schema Source #
Some String
.
The unexported A
typeclass exists to overload someString
with precicely
two possible types:
someString
:: (Text
->Bool
) ->Schema
someString
:: (Text
->Bool
) ->Text
->Schema
The optional Text
argument is used for error reporting if validation
fails.
Examples
>>>
schema (someString (\s -> Text.length s > 5)) (String "foobar")
True
>>>
schema (someString (\s -> Text.length s > 5)) (String "foo")
False
regex :: Text -> Schema Source #
A String
that matches a regular expression.
Examples
>>>
schema (regex "a+b") (String "xaaabx")
True
>>>
schema (regex "c{2}") (String "cd")
False
Object schemas
An arbitrarily deep non-empty Path
into an Object
, created with either
string-literal or list-literal syntax.
Beware: the IsList
instance is partial; []
is not allowed and will
call error
.
Examples
>>>
"foo" :: Path
["foo"]
>>>
["foo", "bar"] :: Path
["foo","bar"]
>>>
[] :: Path
*** Exception: Data.Aeson.Validation.Path.fromList: empty list
object :: [Field] -> Schema Source #
An Object
, possibly with additional fields.
To match any Object
, use
.object
[]
Examples
>>>
let fields = ["foo" .: number]
>>>
let values = ["foo" .= Number 1, "bar" .= Bool True]
>>>
schema (object fields) (Object values)
True
>>>
let fields = [["foo", "bar"] .: number]
>>>
let values = ["foo" .= Object ["bar" .= Number 1]]
>>>
schema (object fields) (Object values)
True
object' :: [Field] -> Schema Source #
An Object
with no additional fields.
The '
mark means "strict" as in foldl'
, because object'
matches
Object
s more strictly than object
.
Examples
>>>
let fields = ["foo" .: number]
>>>
let values = ["foo" .= Number 1]
>>>
schema (object' fields) (Object values)
True
>>>
let fields = ["foo" .: number]
>>>
let values = ["foo" .= Number 1, "bar" .= Bool True]
>>>
schema (object' fields) (Object values)
False
>>>
let fields = [["foo", "bar"] .: number]
>>>
let values = ["foo" .= Object ["bar" .= Number 1]]
>>>
schema (object' fields) (Object values)
True
>>>
let fields = [["foo", "bar"] .: number]
>>>
let values = ["foo" .= Object ["bar" .= Number 1], "baz" .= Bool True]
>>>
schema (object' fields) (Object values)
False
Array schemas (homogenous lists)
array :: Schema -> Schema Source #
A "homogenous" Array
of any size.
The array need not be truly homogenous; it simply has the same Schema
applied to each element. However, the Schema
could be anything
, or
composed of many alternatives using <>
.
Examples
>>>
schema (array bool) (Array [Bool True, Bool False])
True
>>>
schema (array anything) (Array [Bool True, String "foo"])
True
>>>
schema (array integer) (Array [Number 1.5])
False
Set schemas (homogenous, unique lists)
set :: Schema -> Schema Source #
A "homogenous" (see note above), unique Array
of any size.
Examples
>>>
schema (set bool) (Array [Bool True])
True
>>>
schema (set bool) (Array [Bool True, Bool True])
False