Validator Pattern

Inspects the elements gathered by a builder and confirms an object's constraints are satisfied before the immutable instance is produced.

Overview

Each OsidObject has a corresponding OsidObjectValidator that inspects and checks the compliance of an OsidObject. It is used by default in the builder pattern but can be used for any OsidObject before releasing it to an OSID Consumer.

Which checks a validator runs is selected by an EnumSet<Validation>. ALL is the default. The Validation enum enumerates the available check types; passing no enum set applies a default validation.

Validation Checks

Each constant of the Validation enum names one class of check a validator may apply. ALL and NONE select every check or none; the remainder can be combined in any EnumSet.

ValueDefinition
ALLall checks
NONEthe opposite of ALL
NULLcheck for any nulls
NIL_IDcheck for any nil implementations of Id
NIL_TYPEcheck for any nil implementations of Type
NIL_OBJECTcheck for any nil implementations of objects (not implemented)
RANGEcheck that low/high and min/max ranges are in the right order
CARDINALcheck that cardinal numbers are non-negative
TYPE_SUPPORTcross-reference type support
EMPTY_STRINGlisted in the enumeration but not a check
ID_CONSISTENCYcheck for consistent Ids
METHOD_SUPPORTcheck for proper method states (basic isActive() and isEnabled() boolean checks)
TRAVERSEtraverse nested OsidObjects
RECORDScheck for existence of OsidRecords by Type

Javadoc Reference

net.okapia.osid.jamocha.builder.validator

Static Factory Method Pattern

static OsidObject validateOsidObject(EnumSet<Validation> validation, OsidObject object)
ParameterTypeDescription
validation EnumSet<Validation> the set of checks to perform; an overload without this argument applies a default validation
object OsidObject the object to validate

Returns the validated OsidObject unchanged — throws InvalidReturnException, NullReturnException, BadLogicException, or NullArgumentException when a constraint is violated

Example

public org.osid.repository.Asset getAsset(org.osid.id.Id assetId)
    throws org.osid.NotFoundException,
           org.osid.OperationFailedException,
           org.osid.PermissionDeniedFailedException {

    var data = fetch(assetId);
    return (new (AssetValidator.validate(new MyAsset(data));
}
        

Yes, I run them inline where possible. In most cases it is worth it to blow up where the problem was caused.