StringLengthConstraint extends AbstractStringConstraint { @Override public Violation typedErrorFor(String value) { int length = value == null ? 0 : value.length(); if (minLength != null && minLength > length) { return new Violation(this, '\'' + value + "' is too short, must be at least " + minLength + " chars"); } if (maxLength != null && maxLength < length) { return new Violation(this, '\'' + value + "' is too long, must no more than " + maxLength + " chars"); } return null; } StringLengthConstraint(String theId, boolean nullAllowed, Integer theMin, Integer theMax); @Override String typeTag(); @Override Map<Parameter<?>, Object> parameters(); @Override Violation typedErrorFor(String value); @Override String toString(); static final String TypeTag; static final String Docs; static final IntegerParameter MIN_LENGTH; static final IntegerParameter MAX_LENGTH; static final ConstraintBuilder<String> Builder; }
@Test public void typedErrorFor() { StringLengthConstraint slc = new StringLengthConstraint("", true, 5, 10); Violation v = slc.errorFor(null); assertNull(v); v = slc.errorFor("billy"); assertNull(v); v = slc.errorFor("bill"); assertNotNull(v); v = slc.errorFor("bill bill bill bill"); assertNotNull(v); }