
com.github.fge.jsonschema.processing.validation.ValidationChain Maven / Gradle / Ivy
/*
* Copyright (c) 2013, Francis Galiegue
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Lesser GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Lesser GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package com.github.fge.jsonschema.processing.validation;
import com.github.fge.jsonschema.library.Library;
import com.github.fge.jsonschema.messages.SyntaxMessages;
import com.github.fge.jsonschema.processing.ProcessingException;
import com.github.fge.jsonschema.processing.Processor;
import com.github.fge.jsonschema.processing.ProcessorChain;
import com.github.fge.jsonschema.processing.ValidationData;
import com.github.fge.jsonschema.processing.build.FullValidationContext;
import com.github.fge.jsonschema.processing.build.ValidatorBuilder;
import com.github.fge.jsonschema.processing.digest.SchemaDigester;
import com.github.fge.jsonschema.processing.format.FormatProcessor;
import com.github.fge.jsonschema.processing.selector.ProcessorSelector;
import com.github.fge.jsonschema.processing.syntax.SyntaxProcessor;
import com.github.fge.jsonschema.report.ProcessingMessage;
import com.github.fge.jsonschema.report.ProcessingReport;
import com.google.common.base.Predicate;
public final class ValidationChain
implements Processor
{
private final Processor processor;
public ValidationChain(final Library library)
{
final SyntaxProcessor syntaxProcessor
= new SyntaxProcessor(library.getSyntaxCheckers());
final SchemaDigester digester
= new SchemaDigester(library.getDigesters());
final ValidatorBuilder builder
= new ValidatorBuilder(library.getValidators());
final FormatProcessor formatProcessor
= new FormatProcessor(library.getFormatAttributes());
final Processor onSuccess
= ProcessorChain.startWith(digester).chainWith(builder)
.chainWith(formatProcessor).end();
final ProcessorSelector selector
= new ProcessorSelector()
.when(schemaIsValid()).then(onSuccess)
.otherwise(onFailure());
processor = ProcessorChain.startWith(syntaxProcessor)
.chainWith(selector.getProcessor()).end();
}
@Override
public FullValidationContext process(final ProcessingReport report,
final ValidationData input)
throws ProcessingException
{
return processor.process(report, input);
}
private static Processor onFailure()
{
return new Processor()
{
@Override
public FullValidationContext process(final ProcessingReport report,
final ValidationData input)
throws ProcessingException
{
final ProcessingMessage message = input.newMessage()
.msg(SyntaxMessages.INVALID_SCHEMA);
throw new ProcessingException(message);
}
};
}
private static Predicate schemaIsValid()
{
return new Predicate()
{
@Override
public boolean apply(final ValidationData input)
{
return input.getSchema().isValid();
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy