1602 / json-schema / Json.Schema.Validation

Validate

Validation fails with list of errors, one for each invalid leaf of the value object. When validation succeeds it also returns value being validated. Currently this value is the same as initial value, later version will allow options to be supplied in order to normalize value along the validation (e.g. apply defaults, remove additional properties, coerce types)

validate : ValidationOptions -> Ref.SchemataPool -> Json.Decode.Value -> Json.Schema.Definitions.Schema -> Json.Schema.Definitions.Schema -> Result (List Error) Json.Decode.Value

Validate value against schema


type alias ValidationOptions =
{ applyDefaults : Basics.Bool }

Validation options which allow to apply defaults (more options upcoming)

defaultOptions : ValidationOptions

Default validation options, applyDefaults = True

Validation Errors


type alias Error =
{ jsonPointer : JsonPointer
, details : ValidationError 
}

Attempt to validate returns Result with list of Error instances as an Err.


type ValidationError
    = MultipleOf Basics.Float Basics.Float
    | Maximum Basics.Float Basics.Float
    | Minimum Basics.Float Basics.Float
    | ExclusiveMaximum Basics.Float Basics.Float
    | ExclusiveMinimum Basics.Float Basics.Float
    | MaxLength Basics.Int Basics.Int
    | MinLength Basics.Int Basics.Int
    | Pattern String String
    | MaxItems Basics.Int Basics.Int
    | MinItems Basics.Int Basics.Int
    | UniqueItems Json.Decode.Value
    | Contains
    | MaxProperties Basics.Int Basics.Int
    | MinProperties Basics.Int Basics.Int
    | Required (List String)
    | RequiredProperty
    | AdditionalPropertiesDisallowed (List String)
    | AdditionalPropertyDisallowed
    | InvalidPropertyName (List Error)
    | Enum
    | Const
    | InvalidType String
    | OneOfNoneSucceed
    | OneOfManySucceed Basics.Int
    | Not
    | UnresolvableReference String
    | AlwaysFail

Validation errors with details. The rule of parametrized errors like Maximum is that first parameter is always expected value, second parameter is actual value. Most of errors named after respective validation properties, only exception from this rule for cases like AlwaysFail which doesn't have keyword (this is result of boolean schema false), or AdditionalPropertiesDisallowed which represent subset of .additionalProperties validation when its value equals to false and additional property is present.

There are keywords in JSON Schema which don't have their dedicated error codes:

The reason for this is the nature of these errors is to go deeper into the nested Schema and Value.

Current implementation of validation only creates errors for leaves of the Value, not for nodes, e.g. if one of the properties fails a validation, error list will contain an error for the property but not for the object containing it. This decision is made to reduce noise in errors, since it is obvious that all the parent objects containing invalid properties are also invalid, and this information can be derived from json path if needed.


type alias JsonPointer =
{ ns : String
, path : List String 
}

Path in json value.

A few notes: