Teki
Rules

Optional

Skip validation when supported fields are missing.

Use optional() when a field may be absent, but should still be validated if present.

Teki schema = Teki.fromRules(
    string("website").optional().url(),
    object("metadata").optional().fields(
        string("source").optional().max(40)
    ),
    array("tags").optional().elements().string()
);

Available on

  • string(...)
  • object(...)
  • array(...)

Annotation behavior

When using Teki.from(Class<?>), fields without @Required are treated as optional automatically.

Annotation equivalent

Use @Optional to make the intent explicit when no other presence annotation is present. Fields without @Required are already optional by default, but @Optional documents that this is deliberate.

public class UserForm {
    @Required
    String username;

    @Optional
    @Email
    String email; // optional, but validated as email if present
}

On this page