#
Custom validationsThis tutorial shows you how to extend the validations capabilities of AML-defined metadata by writing custom validations that can be enforced by AMF.
#
Before you beginPrerequisites:
- JVM: version 7 or higher
- SBT to build the AMF command-line
#
Download the exampleYou can download the example from the AML project examples repository in Github.
#
Build and install the AMF command-line toolThis tutorial assumes you have a working version of the AMF command-line as described in the quickstart tutorial.
#
Standard validationAML Dialects encode a mapping from the AST of a YAML/JSON document to a metadata graph and a set of constraints over the shape of that graph. These constraints are formally defined in the AML specification using a W3C recommendation known as SHACL.
AMF or any other SHACL compliant processor can validate the graph of data parsed from an AML document using the set of SHACL constraints parsed from the associated AML Dialect.
The AML Dialect Playlist 1.0
located at aml/examples/music/dialect/playlist.yaml
defines a metadata document to encode musical playlists:
playlist.yaml
The dialect provides the semantics of the metadata using two AML Vocabularies for music and music curation, located at aml/music/vocabulary/music.yaml
and aml/music/vocabulary/music_curation.yaml
.
Using this dialect, we can describe document instances encoding playlist information like the one located in aml/music/playlist1.yaml
.
playlist1.yaml
AMF can be used to validate any AML document instance using the validate
command:
#
Validation profilesAdditional validation constraints can be defined over the parsed graph of metadata using a validation profile.
Validation profiles are documents defined after describing an AML Dialect for validation profiles.
Validation profile documents can be used to define constraints declaratively and set the severity level of these validations. Additionally, programmatic constraints in JavaScript can also be defined.
An example of a validation profile can be found at aml/music/custom_validations/boring.yaml
. This profile adds validation errors for every single song with a duration longer than 180 seconds:
boring.yaml
As an example, the constraint has been declared in two different ways. too-long1
defines the constraint declaratively, using the syntax defined in the Validation Profile
dialect.
The validation too-long2
is the equivalent validation, but using custom JavaScript code to check the constraint.
Both constraints target nodes in the parsed graph, using the semantics defined in the Music
AML Vocabulary: the class music.Track
and the property music.duration
.
This validation profile can be used in the AMF command line tool using the --custom-validation-profile
argument: