Custom validations

AMF can be used to validate parsed AML models with custom validation profiles. Before using the custom validation, a Validation Profile valid document instance must be registered using the loadValidationProfile function.

The name of the validation profile must be passed to the validate function for the custom validations to be applied

Example

Java
JavaScript
package aml_org.examples;

import amf.client.AMF;
import amf.client.model.document.DialectInstance;
import amf.client.parse.Aml10Parser;
import amf.client.validate.ValidationReport;
import amf.client.validate.ValidationResult;

import java.util.concurrent.ExecutionException;

import static java.lang.System.out;

public class CustomValidation {

    //#custom-validation-example
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        AMF.init().get();

        AMF.registerDialect("file://aml/music/dialect/playlist.yaml").get();

        DialectInstance model = (DialectInstance) new Aml10Parser().parseFileAsync("file://aml/music/playlist1.yaml").get();

        AMF.loadValidationProfile("file://aml/music/custom_validations/boring.yaml").get();

        ValidationReport report = AMF.validate(model, "Boring Playlists", "AMF").get();

        out.println("Validates? " + report.conforms());
        if (!report.conforms()) {
            out.println("Errors:");
            for (ValidationResult result : report.results()) {
                out.println(" - " + result.message() + " => " + result.targetNode());
            }
        }
    }
}


var amf = require("amf-client-js");
var AMF = amf.AMF;
var Aml10Parser = amf.Aml10Parser;

//#custom-validation-example
AMF.init()
    .then(function() {
        return AMF.registerDialect("file://aml/music/dialect/playlist.yaml");
    })
    .then(function() {
        return AMF.loadValidationProfile("file://aml/music/custom_validations/boring.yaml");
    })
    .then(function() {
        return new Aml10Parser()
            .parseFileAsync("file://aml/music/playlist1.yaml");
    })
    .then(function(unit) {
        return AMF.validate(unit, "Boring Playlists", "AMF");
    })
    .then(function(report) {
        console.log("Validates? " + report.conforms);
        if (!report.conforms) {
            out.println("Errors:");
            report.results.forEach(function (result) {
                console.log(" - " + result.message + " => " + result.targetNode);
            });
        }
    });

results matching ""

    No results matching ""