Search in sources :

Example 6 with ISOVException

use of org.jpos.iso.validator.ISOVException in project jPOS by jpos.

the class ISOFieldValidator method validate.

/**
 * Validate a field component. Default for fields only consider
 * field length validations.
 * @param c ISOField component
 * @return an ISOComponent result of validation process. If there area any
 * validation error, then an ISOV component replace original c and it's
 * returned in case of break-on-error condition is false. If break-on-error
 * is false, then an ISOVException containing the ISOV component is raised.
 * @throws ISOException if there are some errors during validation.
 * It contains an ISOV component inside referencing the errors.
 */
public ISOComponent validate(ISOComponent c) throws ISOException {
    ISOField f = (ISOField) c;
    Object v = f.getValue();
    int l = 0;
    if (v instanceof byte[])
        l = ((byte[]) v).length;
    else if (v instanceof String)
        l = ((String) v).length();
    if (l < minLen || l > maxLen) {
        ISOVError e = new ISOVError("Invalid Length Error. Length must be in [" + minLen + ", " + maxLen + "]. (Current len: " + l + ") ", getRejCode(ISOVError.ERR_INVALID_LENGTH));
        if (f instanceof ISOVField)
            ((ISOVField) f).addISOVError(e);
        else
            f = new ISOVField(f, e);
        if (breakOnError)
            throw new ISOVException("Error on field " + f.getKey(), f);
    }
    return f;
}
Also used : ISOVException(org.jpos.iso.validator.ISOVException)

Aggregations

ISOVException (org.jpos.iso.validator.ISOVException)6 ISOBaseValidator (org.jpos.iso.ISOBaseValidator)3 ISOFieldValidator (org.jpos.iso.ISOFieldValidator)3 ISOMsg (org.jpos.iso.ISOMsg)2 MSGTEST (org.jpos.iso.validator.MSGTEST)2 LogEvent (org.jpos.util.LogEvent)2 Test (org.junit.Test)2 Map (java.util.Map)1 ISOComponent (org.jpos.iso.ISOComponent)1 ISOException (org.jpos.iso.ISOException)1 ISOValidator (org.jpos.iso.ISOValidator)1 IVA_ALPHANUM (org.jpos.iso.IVA_ALPHANUM)1 IVA_ALPHANUMNOBLANK (org.jpos.iso.IVA_ALPHANUMNOBLANK)1