1.2 library(json_schema): JSON Schema reader and validator
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog JSON library
        • Supporting JSON
          • library(json_schema): JSON Schema reader and validator
            • Status
            • Predicates
              • json_validate/3
              • json_compile_schema/3
              • json_check/3

1.2.2 Predicates

[det]json_validate(+SchemaFile, +DataDict, +Options)
Given a file holding a JSON Schema and a Prolog dict holding JSON data, validate the data against the schema. Options are passed to json_compile_schema/3 and json_check/3.
throws
error(Formal, json_path(Path)), where Path is a list of properties from the root element to the culprit element. Formal is typically a type, domain or existence error. This file contains the message hooks to generate a human readable error from these exceptions using print_message/2.
[det]json_compile_schema(+Input, -Type, +Options)
Load and translated a JSON Schema. Input is either a file name, a specification for absolute_file_name/3 or the output of json_read_dict/2.

If Input is a file name, the loaded and compiled schema is cached. Reusing the cache validates the modification file of the schema file and reloads it if the file's time stamp has changed. Note that true and false are valid schemas and cannot be used as file names.

[semidet]json_check(+Spec, ?JSON, +Options)
Validate a JSON object. Spec is a Prolog representation of the schema that is optimized for validation. This representation is derived from JSON data using json_compile_schema/3. Options:
on_error(Mode)
What to do if an error is found. Defined modes are
error
Raise an exception. This is the default. Note that only the first error is reported this way.
warning
Print a message
silent
Fail
value_string_as(Type)
Same as for json_read/3.

This predicate is often used through validate_json_dict/3, which mantains a cached mapping from the JSON Schema to Spec.