Path: redb-route/redb.Route/Definitions/ValidateDefinition.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/Definitions/ValidateDefinition.cs
----
using Json.Schema;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Validation;
using System.Xml.Schema;
namespace redb.Route.Definitions;
///
/// Leaf definition that validates the exchange using an instance.
///
public sealed class ValidateInstanceDefinition : ProcessorDefinition
{
private readonly IMessageValidator _validator;
private readonly bool _throwOnFailure;
/// Gets the validator instance.
public IMessageValidator Validator => _validator;
/// Gets whether a validation failure throws an exception.
public bool ThrowOnFailure => _throwOnFailure;
/// Creates a validate definition from a validator instance.
public ValidateInstanceDefinition(IMessageValidator validator, bool throwOnFailure = true)
{
ArgumentNullException.ThrowIfNull(validator);
_validator = validator;
_throwOnFailure = throwOnFailure;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(_validator, _throwOnFailure);
}
///
/// Leaf definition that validates the exchange using a predicate function.
///
public sealed class ValidatePredicateDefinition : ProcessorDefinition
{
private readonly IPredicate _predicate;
private readonly string _errorMessage;
private readonly bool _throwOnFailure;
/// Gets the error message used when validation fails.
public string ErrorMessage => _errorMessage;
/// Gets whether a validation failure throws an exception.
public bool ThrowOnFailure => _throwOnFailure;
/// Creates a validate definition from a predicate.
public ValidatePredicateDefinition(IPredicate predicate, string errorMessage, bool throwOnFailure = true)
{
ArgumentNullException.ThrowIfNull(predicate);
ArgumentException.ThrowIfNullOrWhiteSpace(errorMessage);
_predicate = predicate;
_errorMessage = errorMessage;
_throwOnFailure = throwOnFailure;
}
/// Creates a validate definition from a delegate, wrapped as a .
public ValidatePredicateDefinition(Func predicate, string errorMessage, bool throwOnFailure = true)
: this(new Predicates.LambdaPredicate(predicate ?? throw new ArgumentNullException(nameof(predicate))), errorMessage, throwOnFailure)
{
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(new PredicateValidator(_predicate, _errorMessage), _throwOnFailure);
}
///
/// Leaf definition that validates the exchange body against a JSON Schema specified as a string.
///
public sealed class ValidateJsonSchemaStringDefinition : ProcessorDefinition
{
private readonly string _schemaJson;
private readonly bool _throwOnFailure;
/// Gets the JSON Schema string.
public string SchemaJson => _schemaJson;
/// Gets whether a validation failure throws an exception.
public bool ThrowOnFailure => _throwOnFailure;
/// Creates a validate definition from a JSON Schema string.
public ValidateJsonSchemaStringDefinition(string schemaJson, bool throwOnFailure = true)
{
ArgumentException.ThrowIfNullOrWhiteSpace(schemaJson);
_schemaJson = schemaJson;
_throwOnFailure = throwOnFailure;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(new JsonSchemaValidator(_schemaJson), _throwOnFailure);
}
///
/// Leaf definition that validates the exchange body against a object.
///
public sealed class ValidateJsonSchemaObjectDefinition : ProcessorDefinition
{
private readonly JsonSchema _schema;
private readonly bool _throwOnFailure;
/// Creates a validate definition from a JsonSchema object.
public ValidateJsonSchemaObjectDefinition(JsonSchema schema, bool throwOnFailure = true)
{
ArgumentNullException.ThrowIfNull(schema);
_schema = schema;
_throwOnFailure = throwOnFailure;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(new JsonSchemaValidator(_schema), _throwOnFailure);
}
///
/// Leaf definition that validates the exchange body against an XSD schema string.
///
public sealed class ValidateXsdStringDefinition : ProcessorDefinition
{
private readonly string _xsdContent;
private readonly bool _throwOnFailure;
/// Gets the XSD content string.
public string XsdContent => _xsdContent;
/// Gets whether a validation failure throws an exception.
public bool ThrowOnFailure => _throwOnFailure;
/// Creates a validate definition from an XSD content string.
public ValidateXsdStringDefinition(string xsdContent, bool throwOnFailure = true)
{
ArgumentException.ThrowIfNullOrWhiteSpace(xsdContent);
_xsdContent = xsdContent;
_throwOnFailure = throwOnFailure;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(new XsdValidator(_xsdContent), _throwOnFailure);
}
///
/// Leaf definition that validates the exchange body against an XSD schema with a target namespace.
///
public sealed class ValidateXsdNamespaceDefinition : ProcessorDefinition
{
private readonly string? _targetNamespace;
private readonly string _xsdContent;
private readonly bool _throwOnFailure;
/// Gets the target XML namespace (may be null).
public string? TargetNamespace => _targetNamespace;
/// Gets the XSD content string.
public string XsdContent => _xsdContent;
/// Gets whether a validation failure throws an exception.
public bool ThrowOnFailure => _throwOnFailure;
/// Creates a validate definition with a namespace-qualified XSD.
public ValidateXsdNamespaceDefinition(string? targetNamespace, string xsdContent, bool throwOnFailure = true)
{
ArgumentException.ThrowIfNullOrWhiteSpace(xsdContent);
_targetNamespace = targetNamespace;
_xsdContent = xsdContent;
_throwOnFailure = throwOnFailure;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(new XsdValidator(_targetNamespace, _xsdContent), _throwOnFailure);
}
///
/// Leaf definition that validates the exchange body against a pre-built .
///
public sealed class ValidateXsdSchemaSetDefinition : ProcessorDefinition
{
private readonly XmlSchemaSet _schemaSet;
private readonly bool _throwOnFailure;
/// Creates a validate definition from an XmlSchemaSet.
public ValidateXsdSchemaSetDefinition(XmlSchemaSet schemaSet, bool throwOnFailure = true)
{
ArgumentNullException.ThrowIfNull(schemaSet);
_schemaSet = schemaSet;
_throwOnFailure = throwOnFailure;
}
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(new XsdValidator(_schemaSet), _throwOnFailure);
}
///
/// Leaf definition that validates the exchange body against an XSD file, its imports and includes read
/// relative to it (see ). The path resolves through the
/// context's resource resolver when the route is built, as the xslt step's file does, so a schema inside a
/// package is found where the package keeps its resources.
///
public sealed class ValidateXsdFileDefinition : ProcessorDefinition
{
/// Creates a validate definition from an XSD file.
public ValidateXsdFileDefinition(string schemaPath, string? targetNamespace = null, string? schemaRoot = null,
bool throwOnFailure = true)
{
ArgumentException.ThrowIfNullOrWhiteSpace(schemaPath);
SchemaPath = schemaPath;
TargetNamespace = targetNamespace;
SchemaRoot = schemaRoot;
ThrowOnFailure = throwOnFailure;
}
/// Gets the schema file as written.
public string SchemaPath { get; }
/// Gets the target namespace; null takes the schema's own.
public string? TargetNamespace { get; }
/// Gets the directory imports stay under, relative to the schema's directory; null is that directory.
public string? SchemaRoot { get; }
/// Gets whether a validation failure throws an exception.
public bool ThrowOnFailure { get; }
///
public override IProcessor CreateProcessor(IRouteContext context)
=> new ValidateProcessor(
XsdValidator.FromFile(ResourceResolution.Resolve(context, SchemaPath, "XSD schema"), TargetNamespace, SchemaRoot),
ThrowOnFailure);
}