json-tools/json-schema/1.0.1

NOTE: the latest version is 1.0.2

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)

ValidationOptions
-> SchemataPool
-> Value
-> Schema
-> Schema
-> Result (List Error) Value

Validate value against schema

type alias ValidationOptions =
{ applyDefaults : 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.

= MultipleOf Float Float
| Maximum Float Float
| Minimum Float Float
| ExclusiveMaximum Float Float
| ExclusiveMinimum Float Float
| MaxLength Int Int
| MinLength Int Int
| Pattern String String
| MaxItems Int Int
| MinItems Int Int
| UniqueItems Value
| Contains
| MaxProperties Int Int
| MinProperties Int Int
| Required (List String)
| RequiredProperty
| AdditionalPropertiesDisallowed (List String)
| AdditionalPropertyDisallowed
| InvalidPropertyName (List Error)
| Enum
| Const
| InvalidType String
| OneOfNoneSucceed
| OneOfManySucceed 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:

  • items
  • additionalItems
  • properties
  • patternProperties
  • dependencies
  • allOf
  • oneOf

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:

  • empty list represents root
  • indices of array items are integers encoded as string