Blog of Raivo Laanemets

Stories about web development, consulting and personal computers.

Announcement: dict schema validation library for SWI-Prolog

On 2014-03-25

Last couple of days I have been working on a library for SWI-Prolog 7.x that validates various types of terms. It started from the need of converting some JSON property values (that are initially strings) into atoms. Now it has grown into a term validator that takes many features from the JSON-Schema standard.

Here is an example schema for vehicles:

?- register_schema(vehicle, _{
    type: dict,
    keys: _{
        year: _{ type: integer, min: 1672 },
        make: _{ type: atom, min_length: 1 },
        model: _{ type: atom, min_length: 1 }
    }
}).

With this I can now validate a dict:

Vehicle = vehicle{
    year: 1200,
    make: chevrolet,
    model: corvette
},
convert(Vehicle, vehicle, Out, Errors).
Out = vehicle{
    make: chevrolet,
    model: corvette,
    year: 1200
},
Errors = [min(#/year, 1200, 1672)].

The error shows that the year value does not satisfy the minimum value.

More examples and the full documentation can be found in the project's README on GitHub.