ByteArraySizeConstraint extends AbstractValueConstraint<byte[]> { @Override public Violation typedErrorFor(byte[] values) { if (minSize != null && minSize > values.length) return new Violation(this, " is too small, minimum size is " + minSize); if (maxSize != null && maxSize < values.length) return new Violation(this, " is too big, maximum size is " + maxSize); return null; } protected  ByteArraySizeConstraint(String theId, boolean nullAllowed, Integer theMinSize, Integer theMaxSize); @Override Map<Parameter<?>, Object> parameters(); @Override byte[] cast(Object value); @Override Violation typedErrorFor(byte[] values); @Override String typeTag(); static final IntegerParameter MIN_SIZE; static final IntegerParameter MAX_SIZE; static final ConstraintBuilder<byte[]> Builder; }
@Test public void typedErrorFor() { Map<String,String> paramsByKey = asParams(ByteArraySizeConstraint.MIN_SIZE, 2, ByteArraySizeConstraint.MAX_SIZE, 4); Collection<Constraint<?>> vcs = ByteArraySizeConstraint.Builder.constraintsFrom(Schema.Type.BYTES, false, paramsByKey); ValueConstraint<?> vc = (ValueConstraint<?>)vcs.iterator().next(); Violation v = vc.errorFor(new byte[] { 0,0,0,0} ); assertNull(v); v = vc.errorFor(new byte[] { 0 } ); assertNotNull(v); v = vc.errorFor(new byte[] { 0, 1, 2, 3, 4 } ); assertNotNull(v); }