Hibernate Validator 中文操作指南

2. Declaring and validating bean constraints

在本章中,您将学习如何声明(请参阅 Section 2.1, “Declaring bean constraints”)和验证(请参阅 Section 2.2, “Validating bean constraints”) Bean 限制。 Section 2.3, “Built-in constraints” 提供了与 Hibernate Validator 集成的所有内置限制的概述。

In this chapter you will learn how to declare (see Section 2.1, “Declaring bean constraints”) and validate (see Section 2.2, “Validating bean constraints”) bean constraints. Section 2.3, “Built-in constraints” provides an overview of all built-in constraints coming with Hibernate Validator.

如果您有兴趣对方法参数和返回值应用约束,请参阅 Chapter 3, Declaring and validating method constraints

If you are interested in applying constraints to method parameters and return values, refer to Chapter 3, Declaring and validating method constraints.

2.1. Declaring bean constraints

Jakarta Bean Validation 中的约束通过 Java 注释表示。在本节中,您将学习如何使用这些注释增强对象模型。有四种类型的 bean 约束:

Constraints in Jakarta Bean Validation are expressed via Java annotations. In this section you will learn how to enhance an object model with these annotations. There are four types of bean constraints:

  1. field constraints

  2. property constraints

  3. container element constraints

  4. class constraints

并非所有约束都可以放置在所有这些级别。事实上,Jakarta Bean Validation 定义的所有默认约束都不能放在类级别。约束注释本身中的 java.lang.annotation.Target 注释确定可以放置约束的元素。有关详细信息,请参阅 Chapter 6, Creating custom constraints

Not all constraints can be placed on all of these levels. In fact, none of the default constraints defined by Jakarta Bean Validation can be placed at class level. The java.lang.annotation.Target annotation in the constraint annotation itself determines on which elements a constraint can be placed. See Chapter 6, Creating custom constraints for more information.

2.1.1. Field-level constraints

约束可以通过注释类的字段来表示。 Example 2.1, “Field-level constraints” 显示了字段级别配置示例:

Constraints can be expressed by annotating a field of a class. Example 2.1, “Field-level constraints” shows a field level configuration example:

示例 2.1:字段级约束

. Example 2.1: Field-level constraints

package org.hibernate.validator.referenceguide.chapter02.fieldlevel;

public class Car {

    @NotNull
    private String manufacturer;

    @AssertTrue
    private boolean isRegistered;

    public Car(String manufacturer, boolean isRegistered) {
        this.manufacturer = manufacturer;
        this.isRegistered = isRegistered;
    }

    //getters and setters...
}

当使用字段级约束时,将使用字段访问策略访问要验证的值。这意味着,即使存在此类访问器,验证引擎也会直接访问实例变量并且不调用属性访问器方法。

When using field-level constraints field access strategy is used to access the value to be validated. This means the validation engine directly accesses the instance variable and does not invoke the property accessor method even if such an accessor exists.

可以将约束应用于任何访问类型的字段(public、private 等)。不过,不支持对静态字段的约束。

Constraints can be applied to fields of any access type (public, private etc.). Constraints on static fields are not supported, though.

验证字节码增强对象时,应使用属性级约束,因为字节码增强库将无法通过反射确定字段访问。

When validating byte code enhanced objects, property level constraints should be used, because the byte code enhancing library won’t be able to determine a field access via reflection.

2.1.2. Property-level constraints

如果您的模型类遵循 JavaBeans 标准,您还可以注释 Bean 类的属性,而不是它的字段。 Example 2.2, “Property-level constraints” 使用了与 Example 2.1, “Field-level constraints” 中相同的实体,但是使用了属性级别约束。

If your model class adheres to the JavaBeans standard, it is also possible to annotate the properties of a bean class instead of its fields. Example 2.2, “Property-level constraints” uses the same entity as in Example 2.1, “Field-level constraints”, however, property level constraints are used.

示例 2.2:属性级约束

. Example 2.2: Property-level constraints

package org.hibernate.validator.referenceguide.chapter02.propertylevel;

public class Car {

    private String manufacturer;

    private boolean isRegistered;

    public Car(String manufacturer, boolean isRegistered) {
        this.manufacturer = manufacturer;
        this.isRegistered = isRegistered;
    }

    @NotNull
    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    @AssertTrue
    public boolean isRegistered() {
        return isRegistered;
    }

    public void setRegistered(boolean isRegistered) {
        this.isRegistered = isRegistered;
    }
}

必须注释属性的 getter 方法,而不是它的 setter。这样,无法写入的仅有 getter 方法的属性也能受到约束。

The property’s getter method has to be annotated, not its setter. That way also read-only properties can be constrained which have no setter method.

使用属性级别约束时,属性访问策略用于访问待验证的值,即验证引擎通过属性访问器方法访问状态。

When using property level constraints property access strategy is used to access the value to be validated, i.e. the validation engine accesses the state via the property accessor method.

建议在一个类中坚持使用 or 属性注释。不建议注释字段 and 和对应的 getter 方法,因为这会导致该字段被验证两次。

It is recommended to stick either to field or property annotations within one class. It is not recommended to annotate a field and the accompanying getter method as this would cause the field to be validated twice.

2.1.3. Container element constraints

可以在参数化类型的类型参数上直接指定约束:这些约束称为容器元素约束。

It is possible to specify constraints directly on the type argument of a parameterized type: these constraints are called container element constraints.

这就要求在约束定义中通过 @Target 来指定 ElementType.TYPE_USE。从 Jakarta Bean Validation 2.0 开始,内置 Jakarta Bean Validation 以及 Hibernate Validator 特定的约束会指定 ElementType.TYPE_USE,并且可以直接在此上下文中使用它们。

This requires that ElementType.TYPE_USE is specified via @Target in the constraint definition. As of Jakarta Bean Validation 2.0, built-in Jakarta Bean Validation as well as Hibernate Validator specific constraints specify ElementType.TYPE_USE and can be used directly in this context.

Hibernate Validator 验证在以下标准 Java 容器上指定的容器元素约束:

Hibernate Validator validates container element constraints specified on the following standard Java containers:

  1. implementations of java.util.Iterable (e.g. _List_s, _Set_s),

  2. implementations of java.util.Map, with support for keys and values,

  3. java.util.Optional, java.util.OptionalInt, java.util.OptionalDouble, java.util.OptionalLong,

  4. the various implementations of JavaFX’s javafx.beans.observable.ObservableValue.

它还支持自定义容器类型上的容器元素约束(请参阅 Chapter 7, Value extraction )。

It also supports container element constraints on custom container types (see Chapter 7, Value extraction).

在 6 以前的版本中,支持容器元素约束的一个子集。在容器级别需要 @Valid 注释来启用它们。从 Hibernate Validator 6 开始不再需要此操作。

In versions prior to 6, a subset of container element constraints were supported. A @Valid annotation was required at the container level to enable them. This is not required anymore as of Hibernate Validator 6.

下面我们展示几个示例,说明各种 Java 类型上的容器元素约束。

We present below a couple of examples illustrating container element constraints on various Java types.

在这些示例中,@ValidPart 是一个自定义约束,允许在 TYPE_USE 上下文中使用。

In these examples, @ValidPart is a custom constraint allowed to be used in the TYPE_USE context.

2.1.3.1. With Iterable

Iterable 类型参数上应用约束时,Hibernate Validator 将验证每个元素。 Example 2.3, “Container element constraint on Set 显示了 Set 和容器元素约束的示例。

When applying constraints on an Iterable type argument, Hibernate Validator will validate each element. Example 2.3, “Container element constraint on Set shows an example of a Set with a container element constraint.

示例 2.3: Set 上的容器元素约束

. Example 2.3: Container element constraint on Set

package org.hibernate.validator.referenceguide.chapter02.containerelement.set;

public class Car {

    private Set<@ValidPart String> parts = new HashSet<>();

    public void addPart(String part) {
        parts.add( part );
    }

    //...

}
Car car = new Car();
car.addPart( "Wheel" );
car.addPart( null );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

assertEquals( 1, constraintViolations.size() );

ConstraintViolation<Car> constraintViolation =
        constraintViolations.iterator().next();
assertEquals(
        "'null' is not a valid car part.",
        constraintViolation.getMessage()
);
assertEquals( "parts[].<iterable element>",
        constraintViolation.getPropertyPath().toString() );

注意属性路径明确指出违规源于可迭代元素。

Note how the property path clearly states that the violation comes from an element of the iterable.

2.1.3.2. With List

List 类型参数上应用约束时,Hibernate Validator 将验证每个元素。 Example 2.4, “Container element constraint on List 显示了带有容器元素约束的 List 的示例。

When applying constraints on a List type argument, Hibernate Validator will validate each element. Example 2.4, “Container element constraint on List shows an example of a List with a container element constraint.

示例 2.4: List 上的容器元素约束

. Example 2.4: Container element constraint on List

package org.hibernate.validator.referenceguide.chapter02.containerelement.list;

public class Car {

    private List<@ValidPart String> parts = new ArrayList<>();

    public void addPart(String part) {
        parts.add( part );
    }

    //...

}
Car car = new Car();
car.addPart( "Wheel" );
car.addPart( null );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

assertEquals( 1, constraintViolations.size() );

ConstraintViolation<Car> constraintViolation =
        constraintViolations.iterator().next();
assertEquals(
        "'null' is not a valid car part.",
        constraintViolation.getMessage()
);
assertEquals( "parts[1].<list element>",
        constraintViolation.getPropertyPath().toString() );

此处,属性路径还包含无效元素的索引。

Here, the property path also contains the index of the invalid element.

2.1.3.3. With Map

容器元素约束也针对映射键和值进行验证。Map 显示了带键约束和值约束的 Example 2.5, “Container element constraint on map keys and values” 示例。

Container element constraints are also validated on map keys and values. Example 2.5, “Container element constraint on map keys and values” shows an example of a Map with a constraint on the key and a constraint on the value.

示例 2.5:地图键和值上的容器元素约束

. Example 2.5: Container element constraint on map keys and values

package org.hibernate.validator.referenceguide.chapter02.containerelement.map;

public class Car {

    public enum FuelConsumption {
        CITY,
        HIGHWAY
    }

    private Map<@NotNull FuelConsumption, @MaxAllowedFuelConsumption Integer> fuelConsumption = new HashMap<>();

    public void setFuelConsumption(FuelConsumption consumption, int value) {
        fuelConsumption.put( consumption, value );
    }

    //...

}
Car car = new Car();
car.setFuelConsumption( Car.FuelConsumption.HIGHWAY, 20 );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

assertEquals( 1, constraintViolations.size() );

ConstraintViolation<Car> constraintViolation =
        constraintViolations.iterator().next();
assertEquals(
        "20 is outside the max fuel consumption.",
        constraintViolation.getMessage()
);
assertEquals(
        "fuelConsumption[HIGHWAY].<map value>",
        constraintViolation.getPropertyPath().toString()
);
Car car = new Car();
car.setFuelConsumption( null, 5 );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

assertEquals( 1, constraintViolations.size() );

ConstraintViolation<Car> constraintViolation =
        constraintViolations.iterator().next();
assertEquals(
        "must not be null",
        constraintViolation.getMessage()
);
assertEquals(
        "fuelConsumption<K>[].<map key>",
        constraintViolation.getPropertyPath().toString()
);

违规的属性路径尤其有趣:

The property paths of the violations are particularly interesting:

  1. The key of the invalid element is included in the property path (in the second example, the key is null).

  2. In the first example, the violation concerns the <map value>, in the second one, the <map key>.

  3. In the second example, you might have noticed the presence of the type argument <K>, more on this later.

2.1.3.4. With java.util.Optional

Optional 的类型参数上应用约束时,Hibernate Validator 将自动解包类型并验证内部值。Optional 显示了带容器元素约束的 Example 2.6, “Container element constraint on Optional” 示例。

When applying a constraint on the type argument of Optional, Hibernate Validator will automatically unwrap the type and validate the internal value. Example 2.6, “Container element constraint on Optional” shows an example of an Optional with a container element constraint.

示例 2.6:可选容器上的容器元素约束

. Example 2.6: Container element constraint on Optional

package org.hibernate.validator.referenceguide.chapter02.containerelement.optional;

public class Car {

    private Optional<@MinTowingCapacity(1000) Integer> towingCapacity = Optional.empty();

    public void setTowingCapacity(Integer alias) {
        towingCapacity = Optional.of( alias );
    }

    //...

}
Car car = new Car();
car.setTowingCapacity( 100 );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

assertEquals( 1, constraintViolations.size() );

ConstraintViolation<Car> constraintViolation = constraintViolations.iterator().next();
assertEquals(
        "Not enough towing capacity.",
        constraintViolation.getMessage()
);
assertEquals(
        "towingCapacity",
        constraintViolation.getPropertyPath().toString()
);

此处,属性路径仅包含属性的名称,因为我们将 Optional 视为“透明”容器。

Here, the property path only contains the name of the property as we are considering Optional as a "transparent" container.

2.1.3.5. With custom container types

自定义容器也可以使用包含元素约束。

Container element constraints can also be used with custom containers.

ValueExtractor 必须在自定义类型中注册,以便检索要验证的值(请参阅 Chapter 7, Value extraction 以了解有关如何实现自己的 ValueExtractor 及如何注册它的详细信息)。

A ValueExtractor must be registered for the custom type allowing to retrieve the value(s) to validate (see Chapter 7, Value extraction for more information about how to implement your own ValueExtractor and how to register it).

Example 2.7, “Container element constraint on custom container type” 显示了一个带有类型参数约束的自定义参数化类型示例。

Example 2.7, “Container element constraint on custom container type” shows an example of a custom parameterized type with a type argument constraint.

示例 2.7:自定义容器类型上的容器元素约束

. Example 2.7: Container element constraint on custom container type

package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;

public class Car {

    private GearBox<@MinTorque(100) Gear> gearBox;

    public void setGearBox(GearBox<Gear> gearBox) {
        this.gearBox = gearBox;
    }

    //...

}
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;

public class GearBox<T extends Gear> {

    private final T gear;

    public GearBox(T gear) {
        this.gear = gear;
    }

    public Gear getGear() {
        return this.gear;
    }
}
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;

public class Gear {
    private final Integer torque;

    public Gear(Integer torque) {
        this.torque = torque;
    }

    public Integer getTorque() {
        return torque;
    }

    public static class AcmeGear extends Gear {
        public AcmeGear() {
            super( 60 );
        }
    }
}
package org.hibernate.validator.referenceguide.chapter02.containerelement.custom;

public class GearBoxValueExtractor implements ValueExtractor<GearBox<@ExtractedValue ?>> {

    @Override
    public void extractValues(GearBox<@ExtractedValue ?> originalValue, ValueExtractor.ValueReceiver receiver) {
        receiver.value( null, originalValue.getGear() );
    }
}
Car car = new Car();
car.setGearBox( new GearBox<>( new Gear.AcmeGear() ) );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );
assertEquals( 1, constraintViolations.size() );

ConstraintViolation<Car> constraintViolation =
        constraintViolations.iterator().next();
assertEquals(
        "Gear is not providing enough torque.",
        constraintViolation.getMessage()
);
assertEquals(
        "gearBox",
        constraintViolation.getPropertyPath().toString()
);
2.1.3.6. Nested container elements

嵌套包含元素也支持约束。

Constraints are also supported on nested container elements.

在验证 Example 2.8, “Constraints on nested container elements” 中显示的 Car 对象时,将同时执行 @NotNullPartManufacturer 的约束。

When validating a Car object as presented in Example 2.8, “Constraints on nested container elements”, both the @NotNull constraints on Part and Manufacturer will be enforced.

示例 2.8:嵌套容器元素上的约束

. Example 2.8: Constraints on nested container elements

package org.hibernate.validator.referenceguide.chapter02.containerelement.nested;

public class Car {

    private Map<@NotNull Part, List<@NotNull Manufacturer>> partManufacturers =
            new HashMap<>();

    //...
}

2.1.4. Class-level constraints

最后但并非最不重要的一点是,也可以在类级别放置约束。在这种情况下,并非单个属性是验证的对象,而是整个对象。如果验证依赖于对象多个属性之间的相关性,类级别约束非常有用。

Last but not least, a constraint can also be placed on the class level. In this case not a single property is subject of the validation but the complete object. Class-level constraints are useful if the validation depends on a correlation between several properties of an object.

Example 2.9, “Class-level constraint” 中的 Car 类具有两个属性 seatCountpassengers,应该确保乘客列表的条目数不超过可用座位数。为此,在类级别上添加了 @ValidPassengerCount 约束。该约束的验证器可以访问完整的 Car 对象,从而可以比较座位数和乘客数。

The Car class in Example 2.9, “Class-level constraint” has the two attributes seatCount and passengers and it should be ensured that the list of passengers does not have more entries than available seats. For that purpose the @ValidPassengerCount constraint is added on the class level. The validator of that constraint has access to the complete Car object, allowing to compare the numbers of seats and passengers.

请参阅 Section 6.2, “Class-level constraints”,详细了解如何实现此自定义约束。

Refer to Section 6.2, “Class-level constraints” to learn in detail how to implement this custom constraint.

示例 2.9:类级约束

. Example 2.9: Class-level constraint

package org.hibernate.validator.referenceguide.chapter02.classlevel;

@ValidPassengerCount
public class Car {

    private int seatCount;

    private List<Person> passengers;

    //...
}

2.1.5. Constraint inheritance

当一个类实现接口或扩展另一个类时,在父类型上声明的所有约束注释都将以与在类自身上指定的约束相同的方式进行应用。为了让事情更清晰,我们来看看以下示例:

When a class implements an interface or extends another class, all constraint annotations declared on the super-type apply in the same manner as the constraints specified on the class itself. To make things clearer let’s have a look at the following example:

示例 2.10:约束继承

. Example 2.10: Constraint inheritance

package org.hibernate.validator.referenceguide.chapter02.inheritance;

public class Car {

    private String manufacturer;

    @NotNull
    public String getManufacturer() {
        return manufacturer;
    }

    //...
}
package org.hibernate.validator.referenceguide.chapter02.inheritance;

public class RentalCar extends Car {

    private String rentalStation;

    @NotNull
    public String getRentalStation() {
        return rentalStation;
    }

    //...
}

此处,类 RentalCarCar 的子类,并添加了属性 rentalStation。如果验证了 RentalCar 实例,不仅会对 rentalStation 上的 @NotNull 约束进行评估,还会对父类上的 manufacturer 约束进行评估。

Here the class RentalCar is a subclass of Car and adds the property rentalStation. If an instance of RentalCar is validated, not only the @NotNull constraint on rentalStation is evaluated, but also the constraint on manufacturer from the parent class.

如果 Car 不是超类而是 RentalCar 实现的接口,则也是如此。

The same would be true, if Car was not a superclass but an interface implemented by RentalCar.

如果覆盖了方法,约束注释也会被聚合。因此,如果 RentalCar 覆盖了 Car 中的 getManufacturer() 方法,除了父类上的 @NotNull 约束外,还会对在覆盖方法上注释的任何约束进行评估。

Constraint annotations are aggregated if methods are overridden. So if RentalCar overrode the getManufacturer() method from Car, any constraints annotated at the overriding method would be evaluated in addition to the @NotNull constraint from the superclass.

2.1.6. Object graphs

Jakarta Bean Validation API 不仅允许验证单个类实例,还允许验证完整的对象图(级联验证)。若要执行此操作,只需使用 @Valid 注释代表对另一个对象的引用的字段或属性,如 Example 2.11, “Cascaded validation” 所示。

The Jakarta Bean Validation API does not only allow to validate single class instances but also complete object graphs (cascaded validation). To do so, just annotate a field or property representing a reference to another object with @Valid as demonstrated in Example 2.11, “Cascaded validation”.

示例 2.11:级联验证

. Example 2.11: Cascaded validation

package org.hibernate.validator.referenceguide.chapter02.objectgraph;

public class Car {

    @NotNull
    @Valid
    private Person driver;

    //...
}
package org.hibernate.validator.referenceguide.chapter02.objectgraph;

public class Person {

    @NotNull
    private String name;

    //...
}

如果验证 Car 的实例,则会将引用的 Person 对象一同验证,因为 driver 字段已用 @Valid 注释。因此,如果引用的 Person 实例的 name 字段为 null,则 Car 的验证将失败。

If an instance of Car is validated, the referenced Person object will be validated as well, as the driver field is annotated with @Valid. Therefore the validation of a Car will fail if the name field of the referenced Person instance is null.

对象图的验证是递归的,即如果标记为级联验证的引用指向一个本身具有用 @Valid 注释的属性的对象,则验证引擎也会对这些引用进行后续处理。验证引擎将确保在级联验证期间不会出现无限循环,例如,如果两个对象相互持有引用。

The validation of object graphs is recursive, i.e. if a reference marked for cascaded validation points to an object which itself has properties annotated with @Valid, these references will be followed up by the validation engine as well. The validation engine will ensure that no infinite loops occur during cascaded validation, for example if two objects hold references to each other.

请注意,在级联验证期间 null 值会遭到忽略。

Note that null values are getting ignored during cascaded validation.

作为约束条件,对象图验证也适用于容器元素。这意味着容器的任何类型参数都可以使用 @Valid 注释,这将导致在验证父对象时验证每个包含的元素。

As constraints, object graph validation also works for container elements. That means any type argument of a container can be annotated with @Valid, which will cause each contained element to be validated when the parent object is validated.

嵌套容器元素也支持级联验证。

Cascaded validation is also supported for nested container elements.

示例 2.12:容器的级联验证

. Example 2.12: Cascaded validation of containers

package org.hibernate.validator.referenceguide.chapter02.objectgraph.containerelement;

public class Car {

    private List<@NotNull @Valid Person> passengers = new ArrayList<Person>();

    private Map<@Valid Part, List<@Valid Manufacturer>> partManufacturers = new HashMap<>();

    //...
}
package org.hibernate.validator.referenceguide.chapter02.objectgraph.containerelement;

public class Part {

    @NotNull
    private String name;

    //...
}
package org.hibernate.validator.referenceguide.chapter02.objectgraph.containerelement;

public class Manufacturer {

    @NotNull
    private String name;

    //...
}

在验证 Example 2.12, “Cascaded validation of containers” 中显示的 Car 类的实例时,将创建 ConstraintViolation

When validating an instance of the Car class shown in Example 2.12, “Cascaded validation of containers”, a ConstraintViolation will be created:

  1. if any of the Person objects contained in the passengers list has a null name;

  2. if any of the Part objects contained in the map keys has a null name;

  3. if any of the Manufacturer objects contained in the list nested in the map values has a null name.

在 6 之前的版本中,Hibernate Validator 支持对容器元素的子集进行级联验证,并且它在容器级别实现(例如,可使用 @Valid private List<Person>Person 启用级联验证)。

In versions prior to 6, Hibernate Validator supported cascaded validation for a subset of container elements and it was implemented at the container level (e.g. you would use @Valid private List<Person> to enable cascaded validation for Person).

这仍然受支持,但并不推荐。请改用容器元素级别 @Valid 注释,因为它更具表现力。

This is still supported but is not recommended. Please use container element level @Valid annotations instead as it is more expressive.

2.2. Validating bean constraints

Validator 接口是 Jakarta Bean Validation 中最重要的对象。下一节介绍如何获取 Validator 实例。然后,您将学习如何使用 Validator 接口的不同方法。

The Validator interface is the most important object in Jakarta Bean Validation. The next section shows how to obtain a Validator instance. Afterwards you’ll learn how to use the different methods of the Validator interface.

2.2.1. Obtaining a Validator instance

验证实体实例的第一步是获取 Validator 实例。获取此实例的途径是通过 Validation 类和 ValidatorFactory。最简单的方法是使用静态方法 Validation#buildDefaultValidatorFactory()

The first step towards validating an entity instance is to get hold of a Validator instance. The road to this instance leads via the Validation class and a ValidatorFactory. The easiest way is to use the static method Validation#buildDefaultValidatorFactory():

示例 2.13: Validation#buildDefaultValidatorFactory()

. Example 2.13: Validation#buildDefaultValidatorFactory()

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
validator = factory.getValidator();

这将以默认配置启动验证器。请参阅 Chapter 9, Bootstrapping 以了解有关不同启动方法和如何获取特定配置 Validator 实例的详细信息。

This bootstraps a validator in the default configuration. Refer to Chapter 9, Bootstrapping to learn more about the different bootstrapping methods and how to obtain a specifically configured Validator instance.

2.2.2. Validator methods

Validator 接口包含三种方法,这些方法可用于验证整个实体或仅验证实体的单个属性。

The Validator interface contains three methods that can be used to either validate entire entities or just single properties of the entity.

所有三种方法都返回 Set<ConstraintViolation>。如果验证成功,则该集合为空。否则,将为每个违反的约束添加一个 ConstraintViolation 实例。

All three methods return a Set<ConstraintViolation>. The set is empty, if the validation succeeds. Otherwise a ConstraintViolation instance is added for each violated constraint.

所有验证方法都有一个 var-args 参数,可用于指定在执行验证时应考虑哪些验证组。如果未指定参数,则使用默认验证组 ( jakarta.validation.groups.Default )。在 Chapter 5, Grouping constraints 中详细讨论了验证组的主题。

All the validation methods have a var-args parameter which can be used to specify which validation groups shall be considered when performing the validation. If the parameter is not specified, the default validation group (jakarta.validation.groups.Default) is used. The topic of validation groups is discussed in detail in Chapter 5, Grouping constraints.

2.2.2.1. Validator#validate()

使用 validate() 方法来执行给定 Bean 的所有约束的验证。 Example 2.14, “Using Validator#validate() 显示了从 Example 2.2, “Property-level constraints” 验证 Car 类的实例,该实例不能满足 manufacturer 属性上的 @NotNull 约束。因此,验证调用返回一个 ConstraintViolation 对象。

Use the validate() method to perform validation of all constraints of a given bean. Example 2.14, “Using Validator#validate() shows the validation of an instance of the Car class from Example 2.2, “Property-level constraints” which fails to satisfy the @NotNull constraint on the manufacturer property. The validation call therefore returns one ConstraintViolation object.

示例 2.14:使用 Validator#validate()

. Example 2.14: Using Validator#validate()

Car car = new Car( null, true );

Set<ConstraintViolation<Car>> constraintViolations = validator.validate( car );

assertEquals( 1, constraintViolations.size() );
assertEquals( "must not be null", constraintViolations.iterator().next().getMessage() );
2.2.2.2. Validator#validateProperty()

借助 validateProperty(),您可以验证给定对象的单个命名属性。属性名称是 JavaBeans 属性名称。

With help of the validateProperty() you can validate a single named property of a given object. The property name is the JavaBeans property name.

示例 2.15:使用 Validator#validateProperty()

. Example 2.15: Using Validator#validateProperty()

Car car = new Car( null, true );

Set<ConstraintViolation<Car>> constraintViolations = validator.validateProperty(
        car,
        "manufacturer"
);

assertEquals( 1, constraintViolations.size() );
assertEquals( "must not be null", constraintViolations.iterator().next().getMessage() );
2.2.2.3. Validator#validateValue()

通过使用 validateValue() 方法,您可以检查给定类的单个属性能否使用指定的值成功验证:

By using the validateValue() method you can check whether a single property of a given class can be validated successfully, if the property had the specified value:

示例 2.16:使用 Validator#validateValue()

. Example 2.16: Using Validator#validateValue()

Set<ConstraintViolation<Car>> constraintViolations = validator.validateValue(
        Car.class,
        "manufacturer",
        null
);

assertEquals( 1, constraintViolations.size() );
assertEquals( "must not be null", constraintViolations.iterator().next().getMessage() );

@Valid 不被 validateProperty()validateValue() 认同。

@Valid is not honored by validateProperty() or validateValue().

例如,Validator#validateProperty() 用于将 Jakarta Bean Validation 集成到 JSF 2(请参阅 Section 11.2, “JSF & Seam”)中,以便在将值传播到模型之前验证输入表单中的值。

Validator#validateProperty() is for example used in the integration of Jakarta Bean Validation into JSF 2 (see Section 11.2, “JSF & Seam”) to perform a validation of the values entered into a form before they are propagated to the model.

2.2.3. ConstraintViolation

2.2.3.1. ConstraintViolation methods

现在是仔细观察 ConstraintViolation 的时候了。通过使用 ConstraintViolation 的不同方法,可以确定有关验证失败原因的大量有用的信息。以下是对这些方法的概述。“示例”列下的值是指 Example 2.14, “Using Validator#validate()

Now it is time to have a closer look at what a ConstraintViolation is. Using the different methods of ConstraintViolation a lot of useful information about the cause of the validation failure can be determined. The following gives an overview of these methods. The values under "Example" column refer to Example 2.14, “Using Validator#validate().

getMessage()

插值错误消息

The interpolated error message

示例“不能为空”

Example"must not be null"

Example

“不能为空”

"must not be null"

getMessageTemplate()

非插值错误消息

The non-interpolated error message

示例“{…​ NotNull.message}”

Example"{…​ NotNull.message}"

Example

“{…​ NotNull.message}”

"{…​ NotNull.message}"

getRootBean()

正在验证的根 bean

The root bean being validated

示例 car

Examplecar

Example

car

getRootBeanClass()

正在验证的根 bean 的类

The class of the root bean being validated

示例 Car.class

Example_Car.class_

Example

Car.class

Car.class

getLeafBean()

如果是一个 bean 约束,则该约束应用于的 bean 实例;如果是一个属性约束,则该约束应用于的属性所在的 bean 实例

If a bean constraint, the bean instance the constraint is applied on; if a property constraint, the bean instance hosting the property the constraint is applied on

示例 car

Example_car_

Example

car

car

getPropertyPath()

从根 bean 到已验证值的属性路径

The property path to the validated value from root bean

示例包含一个类型为 PROPERTY 、名称为“manufacturer”的节点

Examplecontains one node with kind PROPERTY and name "manufacturer"

Example

包含一个类型为 PROPERTY 、名称为 “制造商” 的节点

contains one node with kind PROPERTY and name "manufacturer"

getInvalidValue()

未能通过约束的值

The value failing to pass the constraint

Example_null_

Example

null

null

getConstraintDescriptor()

报告未能通过的约束元数据

Constraint metadata reported to fail

@NotNull 的 Exampledescriptor

Exampledescriptor for @NotNull

Example

@NotNull 的 descriptor

descriptor for @NotNull

2.2.3.2. Exploiting the property path

要确定触发违规的元素,您需要利用 getPropertyPath() 方法的结果。

To determine the element that triggered the violation, you need to exploit the result of the getPropertyPath() method.

返回的 Path 由描述元素路径的 Node 组成。

The returned Path is composed of _Node_s describing the path to the element.

有关 Path 结构,以及 Jakarta Bean Validation 规范的各种 Node_s can be found in link:https://jakarta.ee/specifications/bean-validation/3.0/jakarta-bean-validation-spec-3.0.html#validationapi-constraintviolation[the _ConstraintViolation 分组的详细信息。

More information about the structure of the Path and the various types of Node_s can be found in the _ConstraintViolation section of the Jakarta Bean Validation specification.

2.3. Built-in constraints

Hibernate Validator 由一组常用的基本约束组成。这些约束主要是由 Jakarta Bean Validation 规范定义的(参见 Section 2.3.1, “Jakarta Bean Validation constraints”)。此外,Hibernate Validator 提供了有用的自定义约束(参见 Section 2.3.2, “Additional constraints”)。

Hibernate Validator comprises a basic set of commonly used constraints. These are foremost the constraints defined by the Jakarta Bean Validation specification (see Section 2.3.1, “Jakarta Bean Validation constraints”). Additionally, Hibernate Validator provides useful custom constraints (see Section 2.3.2, “Additional constraints”).

2.3.1. Jakarta Bean Validation constraints

以下提供了 Jakarta Bean 验证 API 中指定的所有约束的列表。所有这些约束均应用于字段/属性级别,Jakarta Bean 验证规范中没有定义任何类级别约束。如果您正在使用 Hibernate 对象关系映射器,那么在为模型创建 DDL 时,会考虑一些约束(请参阅“Hibernate 元数据影响”)。

Below you can find a list of all constraints specified in the Jakarta Bean Validation API. All these constraints apply to the field/property level, there are no class-level constraints defined in the Jakarta Bean Validation specification. If you are using the Hibernate object-relational mapper, some of the constraints are taken into account when creating the DDL for your model (see "Hibernate metadata impact").

Hibernate Validator 允许将一些约束应用到比 Jakarta Bean Validation 规范要求更多的类型的数据(例如,@Max 可以应用到字符串)。依赖此功能可能会影响你的应用程序在 Jakarta Bean Validation 提供程序之间的可移植性。

Hibernate Validator allows some constraints to be applied to more data types than required by the Jakarta Bean Validation specification (e.g. @Max can be applied to strings). Relying on this feature can impact portability of your application between Jakarta Bean Validation providers.

@AssertFalse

检查带注释的元素为 false

Checks that the annotated element is false

支持的数据类型 Booleanboolean

Supported data types_Boolean_, boolean

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

Booleanboolean

Boolean, boolean

Hibernate metadata impact

None

@AssertTrue

检查带注释的元素为 true

Checks that the annotated element is true

支持的数据类型 Booleanboolean

Supported data types_Boolean_, boolean

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

Booleanboolean

Boolean, boolean

Hibernate metadata impact

None

@DecimalMax(value=, inclusive=)

inclusive =false 时,检查带注释的值是否小于指定的最大值。否则,检查值是否小于或等于指定的最大值。参数值是根据 BigDecimal 字符串表示形式的最大值字符串表示形式。

Checks whether the annotated value is less than the specified maximum, when inclusive=false. Otherwise whether the value is less than or equal to the specified maximum. The parameter value is the string representation of the max value according to the BigDecimal string representation.

支持的数据类型 BigDecimalBigIntegerCharSequencebyteshortintlong 及原始类型的相应包装器;HV 另外支持:任何 Numberjavax.money.MonetaryAmount 的子类型(如果 JSR 354 API 和其实现位于类路径中)

Supported data types_BigDecimal_, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of Number and javax.money.MonetaryAmount (if the JSR 354 API and an implementation is on the class path)

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerCharSequencebyteshortintlong 及原始类型的相应包装器;HV 另外支持:任何 Numberjavax.money.MonetaryAmount 的子类型(如果 JSR 354 API 和其实现位于类路径中)

BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of Number and javax.money.MonetaryAmount (if the JSR 354 API and an implementation is on the class path)

Hibernate metadata impact

None

@DecimalMin(value=, inclusive=)

inclusive =false 时,检查带注释的值是否大于指定的最大值。否则,检查值是否大于或等于指定的最大值。参数值是根据 BigDecimal 字符串表示形式的最小值字符串表示形式。

Checks whether the annotated value is larger than the specified minimum, when inclusive=false. Otherwise whether the value is larger than or equal to the specified minimum. The parameter value is the string representation of the min value according to the BigDecimal string representation.

支持的数据类型 BigDecimalBigIntegerCharSequencebyteshortintlong 及原始类型的相应包装器;HV 另外支持:任何 Numberjavax.money.MonetaryAmount 的子类型

Supported data types_BigDecimal_, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerCharSequencebyteshortintlong 及原始类型的相应包装器;HV 另外支持:任何 Numberjavax.money.MonetaryAmount 的子类型

BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

None

@Digits(integer=, fraction=)

检查注释的值是否为一个数字,包含 integer 个数字和 fraction 个小数位

Checks whether the annotated value is a number having up to integer digits and fraction fractional digits

支持的数据类型BigDecimal、 BigIntegerCharSequencebyteshortintlong 和基本类型的相应包装器;此外,HV 支持: Numberjavax.money.MonetaryAmount 的任何子类型

Supported data typesBigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响定义列精度和小数位

Hibernate metadata impactDefines column precision and scale

Supported data types

BigDecimal、 BigIntegerCharSequencebyteshortintlong 和基本类型的相应包装器;此外,HV 支持: Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

定义列精度和小数位

Defines column precision and scale

@Email

检查指定字符序列是否为一个有效的电子邮件地址。可选参数 regexpflags 允许指定一个额外的正则表达式(包括正则表达式标志)供电子邮件匹配。

Checks whether the specified character sequence is a valid email address. The optional parameters regexp and flags allow to specify an additional regular expression (including regular expression flags) which the email must match.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Future

检查注释的日期是否在未来

Checks whether the annotated date is in the future

支持的数据类型_java.util.Date_、 java.util.Calendarjava.time.Instantjava.time.LocalDatejava.time.LocalDateTimejava.time.LocalTimejava.time.MonthDayjava.time.OffsetDateTimejava.time.OffsetTimejava.time.Yearjava.time.YearMonthjava.time.ZonedDateTimejava.time.chrono.HijrahDatejava.time.chrono.JapaneseDatejava.time.chrono.MinguoDatejava.time.chrono.ThaiBuddhistDate ;此外,如果 Joda Time 日期/时间 API 在类路径中,则 HV 支持: ReadablePartialReadableInstant 的任何实现

Supported data types_java.util.Date_, java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

java.util.Datejava.util.Calendarjava.time.Instantjava.time.LocalDatejava.time.LocalDateTimejava.time.LocalTimejava.time.MonthDayjava.time.OffsetDateTimejava.time.OffsetTimejava.time.Yearjava.time.YearMonthjava.time.ZonedDateTimejava.time.chrono.HijrahDatejava.time.chrono.JapaneseDatejava.time.chrono.MinguoDatejava.time.chrono.ThaiBuddhistDate ;此外,如果 Joda Time 日期/时间 API 在类路径中,则 HV 支持: ReadablePartialReadableInstant 的任何实现

java.util.Date, java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate metadata impact

None

@FutureOrPresent

检查注释的日期是否在现在或未来

Checks whether the annotated date is in the present or in the future

支持的数据类型_java.util.Date_、 java.util.Calendarjava.time.Instantjava.time.LocalDatejava.time.LocalDateTimejava.time.LocalTimejava.time.MonthDayjava.time.OffsetDateTimejava.time.OffsetTimejava.time.Yearjava.time.YearMonthjava.time.ZonedDateTimejava.time.chrono.HijrahDatejava.time.chrono.JapaneseDatejava.time.chrono.MinguoDatejava.time.chrono.ThaiBuddhistDate ;此外,如果 Joda Time 日期/时间 API 在类路径中,则 HV 支持: ReadablePartialReadableInstant 的任何实现

Supported data types_java.util.Date_, java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

java.util.Datejava.util.Calendarjava.time.Instantjava.time.LocalDatejava.time.LocalDateTimejava.time.LocalTimejava.time.MonthDayjava.time.OffsetDateTimejava.time.OffsetTimejava.time.Yearjava.time.YearMonthjava.time.ZonedDateTimejava.time.chrono.HijrahDatejava.time.chrono.JapaneseDatejava.time.chrono.MinguoDatejava.time.chrono.ThaiBuddhistDate ;此外,如果 Joda Time 日期/时间 API 在类路径中,则 HV 支持: ReadablePartialReadableInstant 的任何实现

java.util.Date, java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate metadata impact

None

@Max(value=)

检查注释的值是否小于或等于指定的较大值

Checks whether the annotated value is less than or equal to the specified maximum

支持的数据类型_BigDecimal_、 BigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

Supported data types_BigDecimal_, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响为列添加检查约束

Hibernate metadata impactAdds a check constraint on the column

Supported data types

BigDecimalBigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

为列添加检查约束

Adds a check constraint on the column

@Min(value=)

检查注释的值是否大于或等于指定的较小值

Checks whether the annotated value is higher than or equal to the specified minimum

支持的数据类型_BigDecimal_、 BigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

Supported data types_BigDecimal_, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响为列添加检查约束

Hibernate metadata impactAdds a check constraint on the column

Supported data types

BigDecimalBigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

为列添加检查约束

Adds a check constraint on the column

@NotBlank

检查注释的字符序列不为 null 且修剪后的长度大于 0。与 @NotEmpty 的区别在于此约束只能应用于字符序列,并且会忽略尾随空格。

Checks that the annotated character sequence is not null and the trimmed length is greater than 0. The difference to @NotEmpty is that this constraint can only be applied on character sequences and that trailing white-spaces are ignored.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@NotEmpty

检查带注释元素是否既非空也非 null

Checks whether the annotated element is not null nor empty

支持的数据类型_CharSequence_, Collection , Map 和数组

Supported data types_CharSequence_, Collection, Map and arrays

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence , Collection , Map 和数组

CharSequence, Collection, Map and arrays

Hibernate metadata impact

None

@NotNull

检查已注释值是否不等于 null

Checks that the annotated value is not null

支持的数据类型任何类型

Supported data typesAny type

Hibernate 元数据不可空列

Hibernate metadata impactColumn(s) are not nullable

Supported data types

任何类型

Any type

Hibernate metadata impact

列不可空

Column(s) are not nullable

@Negative

检查元素是否严格为负。零值视为无效。

Checks if the element is strictly negative. Zero values are considered invalid.

支持的数据类型_BigDecimal_、 BigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

Supported data types_BigDecimal_, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

None

@NegativeOrZero

检查元素是否为负或零。

Checks if the element is negative or zero.

支持的数据类型_BigDecimal_、 BigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

Supported data types_BigDecimal_, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

None

@Null

检查已注释值是否为 null

Checks that the annotated value is null

支持的数据类型任何类型

Supported data typesAny type

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

任何类型

Any type

Hibernate metadata impact

None

@Past

检查注释日期是否在过去

Checks whether the annotated date is in the past

支持的数据类型_java.util.Date_, java.util.Calendar , java.time.Instant , java.time.LocalDate , java.time.LocalDateTime , java.time.LocalTime , java.time.MonthDay , java.time.OffsetDateTime , java.time.OffsetTime , java.time.Year , java.time.YearMonth , java.time.ZonedDateTime , java.time.chrono.HijrahDate , java.time.chrono.JapaneseDate , java.time.chrono.MinguoDate , java.time.chrono.ThaiBuddhistDate ;HV 也支持,如果 Joda Time 日期/时间 API 在类路径中: ReadablePartialReadableInstant 的所有实现

Supported data types_java.util.Date_,java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; Additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

java.util.Date , java.util.Calendar , java.time.Instant , java.time.LocalDate , java.time.LocalDateTime , java.time.LocalTime , java.time.MonthDay , java.time.OffsetDateTime , java.time.OffsetTime , java.time.Year , java.time.YearMonth , java.time.ZonedDateTime , java.time.chrono.HijrahDate , java.time.chrono.JapaneseDate , java.time.chrono.MinguoDate , java.time.chrono.ThaiBuddhistDate ;HV 也支持,如果 Joda Time 日期/时间 API 在类路径中: ReadablePartialReadableInstant 的所有实现

java.util.Date,java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; Additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate metadata impact

None

@PastOrPresent

检查已注释日期是否在过去或现在

Checks whether the annotated date is in the past or in the present

支持的数据类型_java.util.Date_, java.util.Calendar , java.time.Instant , java.time.LocalDate , java.time.LocalDateTime , java.time.LocalTime , java.time.MonthDay , java.time.OffsetDateTime , java.time.OffsetTime , java.time.Year , java.time.YearMonth , java.time.ZonedDateTime , java.time.chrono.HijrahDate , java.time.chrono.JapaneseDate , java.time.chrono.MinguoDate , java.time.chrono.ThaiBuddhistDate ;HV 也支持,如果 Joda Time 日期/时间 API 在类路径中: ReadablePartialReadableInstant 的所有实现

Supported data types_java.util.Date_,java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; Additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

java.util.Date , java.util.Calendar , java.time.Instant , java.time.LocalDate , java.time.LocalDateTime , java.time.LocalTime , java.time.MonthDay , java.time.OffsetDateTime , java.time.OffsetTime , java.time.Year , java.time.YearMonth , java.time.ZonedDateTime , java.time.chrono.HijrahDate , java.time.chrono.JapaneseDate , java.time.chrono.MinguoDate , java.time.chrono.ThaiBuddhistDate ;HV 也支持,如果 Joda Time 日期/时间 API 在类路径中: ReadablePartialReadableInstant 的所有实现

java.util.Date,java.util.Calendar, java.time.Instant, java.time.LocalDate, java.time.LocalDateTime, java.time.LocalTime, java.time.MonthDay, java.time.OffsetDateTime, java.time.OffsetTime, java.time.Year, java.time.YearMonth, java.time.ZonedDateTime, java.time.chrono.HijrahDate, java.time.chrono.JapaneseDate, java.time.chrono.MinguoDate, java.time.chrono.ThaiBuddhistDate; Additionally supported by HV, if the Joda Time date/time API is on the classpath: any implementations of ReadablePartial and ReadableInstant

Hibernate metadata impact

None

@Pattern(regex=, flags=)

检查带注释的字符串是否与正则表达式 regex 匹配,并考虑给定的标记 match

Checks if the annotated string matches the regular expression regex considering the given flag match

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Positive

检查元素是否严格为正。零值视为无效。

Checks if the element is strictly positive. Zero values are considered invalid.

支持的数据类型_BigDecimal_、 BigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

Supported data types_BigDecimal_, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

None

@PositiveOrZero

检查元素是否为正或零。

Checks if the element is positive or zero.

支持的数据类型_BigDecimal_、 BigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

Supported data types_BigDecimal_, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerbyteshortintlong 和基本类型的相应包装器;此外,HV 支持: CharSequence 的任何子类型(评估由字符序列表示的数字值)、 Numberjavax.money.MonetaryAmount 的任何子类型

BigDecimal, BigInteger, byte, short, int, long and the respective wrappers of the primitive types; additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number and javax.money.MonetaryAmount

Hibernate metadata impact

None

@Size(min=, max=)

检查已注释元素的大小是否在 minmax 之间(包括 minmax

Checks if the annotated element’s size is between min and max (inclusive)

支持的数据类型_CharSequence_, Collection , Map 和数组

Supported data types_CharSequence_, Collection, Map and arrays

Hibernate 元数据 impactColumn 长度将被设置为 max

Hibernate metadata impactColumn length will be set to max

Supported data types

CharSequence , Collection , Map 和数组

CharSequence, Collection, Map and arrays

Hibernate metadata impact

列长度将被设置为 max

Column length will be set to max

除了上面列出的参数外,每个约束都有参数 message、groups 和 payload。这是 Jakarta Bean Validation 规范的要求。

On top of the parameters listed above each constraint has the parameters message, groups and payload. This is a requirement of the Jakarta Bean Validation specification.

2.3.2. Additional constraints

除了 Jakarta Bean 验证 API 定义的约束外,Hibernate 验证器还提供了以下列出的几个有用的自定义约束。除了一个例外外,这些约束也适用于字段/属性级别,只有 @ScriptAssert 是类级别约束。

In addition to the constraints defined by the Jakarta Bean Validation API, Hibernate Validator provides several useful custom constraints which are listed below. With one exception also these constraints apply to the field/property level, only @ScriptAssert is a class-level constraint.

@CreditCardNumber(ignoreNonDigitCharacters=)

检查注释的字符串序列是否通过了 Luhn 校验和测试。请注意,此验证旨在检查用户错误,而不是信用卡有效性!另请参见 Anatomy of a credit card numberignoreNonDigitCharacters 允许忽略非数字字符。默认值为 false

Checks that the annotated character sequence passes the Luhn checksum test. Note, this validation aims to check for user mistakes, not credit card validity! See also Anatomy of a credit card number. ignoreNonDigitCharacters allows to ignore non digit characters. The default is false.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Currency(value=)

检查注释的 javax.money.MonetaryAmount 的货币单位是否属于指定的货币单位。

Checks that the currency unit of the annotated javax.money.MonetaryAmount is part of the specified currency units.

支持的数据类型任何 javax.money.MonetaryAmount 的子类型(如果 JSR 354 API 和一个实现位于类路径中)

Supported data typesany sub-type of javax.money.MonetaryAmount (if the JSR 354 API and an implementation is on the class path)

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

任何 javax.money.MonetaryAmount 的子类型(如果 JSR 354 API 和一个实现位于类路径中)

any sub-type of javax.money.MonetaryAmount (if the JSR 354 API and an implementation is on the class path)

Hibernate metadata impact

None

@DurationMax(days=, hours=, minutes=, seconds=, millis=, nanos=, inclusive=)

检查注释的 java.time.Duration 元素是否不大于由注释参数构造的元素。如果 inclusive 标志设置为 true ,则允许相等。

Checks that annotated java.time.Duration element is not greater than the one constructed from annotation parameters. Equality is allowed if inclusive flag is set to true.

支持的数据类型_java.time.Duration_

Supported data types_java.time.Duration_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

java.time.Duration

java.time.Duration

Hibernate metadata impact

None

@DurationMin(days=, hours=, minutes=, seconds=, millis=, nanos=, inclusive=)

检查注释的 java.time.Duration 元素是否不小于由注释参数构造的元素。如果 inclusive 标志设置为 true ,则允许相等。

Checks that annotated java.time.Duration element is not less than the one constructed from annotation parameters. Equality is allowed if inclusive flag is set to true.

支持的数据类型_java.time.Duration_

Supported data types_java.time.Duration_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

java.time.Duration

java.time.Duration

Hibernate metadata impact

None

@EAN

检查注释的字符串序列是否为有效的 EAN 条形码。type 确定条形码的类型。默认值为 EAN-13。

Checks that the annotated character sequence is a valid EAN barcode. type determines the type of barcode. The default is EAN-13.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@ISBN

检查注释的字符串序列是否为有效的 ISBNtype 确定 ISBN 的类型。默认值为 ISBN-13。

Checks that the annotated character sequence is a valid ISBN. type determines the type of ISBN. The default is ISBN-13.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Length(min=, max=)

验证注释的字符串序列位于 minmax 之间(包括 minmax

Validates that the annotated character sequence is between min and max included

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据 impactColumn 长度将被设置为 max

Hibernate metadata impactColumn length will be set to max

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

列长度将被设置为 max

Column length will be set to max

@CodePointLength(min=, max=, normalizationStrategy=)

验证注释的字符串序列的代码点长度位于 minmax 之间(包括 minmax )。如果 normalizationStrategy 已设置,则验证标准化值。

Validates that code point length of the annotated character sequence is between min and max included. Validates normalized value if normalizationStrategy is set.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@LuhnCheck(startIndex= , endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=)

检查注释的字符串序列中的数字是否通过 Luhn 校验和算法(另请参见 Luhn algorithm )。 startIndexendIndex 允许仅对指定的子字符串运行算法。 checkDigitIndex 允许在字符串序列中使用任意数字作为校验数字。如果未指定,则假设校验数字是指定范围的一部分。最后, ignoreNonDigitCharacters 允许忽略非数字字符。

Checks that the digits within the annotated character sequence pass the Luhn checksum algorithm (see also Luhn algorithm). startIndex and endIndex allow to only run the algorithm on the specified sub-string. checkDigitIndex allows to use an arbitrary digit within the character sequence as the check digit. If not specified it is assumed that the check digit is part of the specified range. Last but not least, ignoreNonDigitCharacters allows to ignore non digit characters.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Mod10Check(multiplier=, weight=, startIndex=, endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=)

检查注释的字符串序列中的数字是否通过通用模 10 校验和算法。 multiplier 确定奇数的乘数(默认为 3), weight 确定偶数的权重(默认为 1)。 startIndexendIndex 允许仅对指定的子字符串运行算法。 checkDigitIndex 允许在字符串序列中使用任意数字作为校验数字。如果未指定,则假设校验数字是指定范围的一部分。最后, ignoreNonDigitCharacters 允许忽略非数字字符。

Checks that the digits within the annotated character sequence pass the generic mod 10 checksum algorithm. multiplier determines the multiplier for odd numbers (defaults to 3), weight the weight for even numbers (defaults to 1). startIndex and endIndex allow to only run the algorithm on the specified sub-string. checkDigitIndex allows to use an arbitrary digit within the character sequence as the check digit. If not specified it is assumed that the check digit is part of the specified range. Last but not least, ignoreNonDigitCharacters allows to ignore non digit characters.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Mod11Check(threshold=, startIndex=, endIndex=, checkDigitIndex=, ignoreNonDigitCharacters=, treatCheck10As=, treatCheck11As=)

检查注释的字符串序列中的数字是否通过模 11 校验和算法。 threshold 指定模 11 乘数增长的阈值;如果未指定值,则乘数将无限增长。 treatCheck10AstreatCheck11As 分别指定当模 11 校验和等于 10 或 11 时要使用的校验数字。分别默认为 X 和 0。 startIndexendIndexcheckDigitIndexignoreNonDigitCharacters 具有与 @Mod10Check 中相同的语义。

Checks that the digits within the annotated character sequence pass the mod 11 checksum algorithm. threshold specifies the threshold for the mod11 multiplier growth; if no value is specified the multiplier will grow indefinitely. treatCheck10As and treatCheck11As specify the check digits to be used when the mod 11 checksum equals 10 or 11, respectively. Default to X and 0, respectively. startIndex, endIndex checkDigitIndex and ignoreNonDigitCharacters carry the same semantics as in @Mod10Check.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Normalized(form=)

验证带注释字符序列是否按照给定的 form 规范化。

Validates that the annotated character sequence is normalized according to the given form.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@Range(min=, max=)

检查带注释的值是否介于 (包括) 指定的最小值与最大值之间

Checks whether the annotated value lies between (inclusive) the specified minimum and maximum

支持的数据类型_BigDecimal_、 BigIntegerCharSequencebyteshortintlong 和基本类型的相应包装器

Supported data types_BigDecimal_, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

BigDecimalBigIntegerCharSequencebyteshortintlong 和基本类型的相应包装器

BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types

Hibernate metadata impact

None

@ScriptAssert(lang=, script=, alias=, reportOn=)

检查给定的脚本是否可以成功用于带注释的元素。为了使用该限制,JSR 223(“适用于 JavaTM 平台的脚本”)定义的 Java 脚本 API 实现必须成为类路径的一部分。要评估的表达式可以用任何脚本或表达式语言编写,为此可以在类路径中找到 JSR 223 兼容引擎。即使这是一个类级约束,也可以使用 reportOn 属性报告特定属性的约束违规,而不是整个对象。

Checks whether the given script can successfully be evaluated against the annotated element. In order to use this constraint, an implementation of the Java Scripting API as defined by JSR 223 ("Scripting for the JavaTM Platform") must be a part of the class path. The expressions to be evaluated can be written in any scripting or expression language, for which a JSR 223 compatible engine can be found in the class path. Even though this is a class-level constraint, one can use the reportOn attribute to report a constraint violation on a specific property rather than the whole object.

支持的数据类型任何类型

Supported data typesAny type

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

任何类型

Any type

Hibernate metadata impact

None

@UniqueElements

检查带注释的集合是否只包含唯一元素。使用 equals() 方法确定相等性。默认消息不包括重复元素列表,但你可以通过覆盖消息并使用 {duplicates} 消息参数来包含它。重复元素列表也包含在约束违规的动态有效负载中。

Checks that the annotated collection only contains unique elements. The equality is determined using the equals() method. The default message does not include the list of duplicate elements but you can include it by overriding the message and using the {duplicates} message parameter. The list of duplicate elements is also included in the dynamic payload of the constraint violation.

支持的数据类型_Collection_

Supported data types_Collection_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

Collection

Collection

Hibernate metadata impact

None

@URL(protocol=, host=, port=, regexp=, flags=)

根据 RFC2396 检查带注释字符序列是否是有效的 URL。如果指定了任何可选参数 protocolhostport ,则相应的 URL 片段必须与指定的值相匹配。可选参数 regexpflags 允许指定一个附加正则表达式(包括正则表达式标记),URL 必须与此相匹配。默认情况下,此约束使用 java.net.URL 构造函数来验证给定的字符串是否表示有效的 URL。同时还有一个基于正则表达式的版本 - RegexpURLValidator - 可以通过 XML(参见 Section 8.2, “Mapping constraints via constraint-mappings )或编程 API(参见 Section 12.15.2, “Adding constraint definitions programmatically” )进行配置。

Checks if the annotated character sequence is a valid URL according to RFC2396. If any of the optional parameters protocol, host or port are specified, the corresponding URL fragments must match the specified values. The optional parameters regexp and flags allow to specify an additional regular expression (including regular expression flags) which the URL must match. Per default this constraint used the java.net.URL constructor to verify whether a given string represents a valid URL. A regular expression based version is also available - RegexpURLValidator - which can be configured via XML (see Section 8.2, “Mapping constraints via constraint-mappings) or the programmatic API (see Section 12.15.2, “Adding constraint definitions programmatically”).

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

@UUID(allowEmpty=, allowNil=, version=, variant=, letterCase=)

根据 RFC 4122 检查带注释字符序列是否是有效的通用唯一标识符。 null 始终有效。选项 allowEmpty 允许空字符序列。 allowNil 包含空 UUID ( 00000000-0000-0000-0000-000000000000 )。 versionvariant 参数控制哪些 UUID 版本和变体被允许。 letterCase 确保小写或大写,但也可以将其配置为不区分大小写。

Checks that the annotated character sequence is a valid universally unique identifier according to RFC 4122. null is always valid. The option allowEmpty allows empty character sequences. allowNil includes nil UUIDs (00000000-0000-0000-0000-000000000000). The version and variant parameters control which UUID versions and variants are allowed. letterCase ensures lower case or upper case, but can also be configured as case insensitive.

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

2.3.2.1. Country specific constraints

Hibernate 验证器还提供一些国家/地区特定约束,例如用于验证社会安全号码的约束。

Hibernate Validator offers also some country specific constraints, e.g. for the validation of social security numbers.

如果你必须实现一个特定国家的约束,可以考虑将其贡献给 Hibernate Validator!

If you have to implement a country specific constraint, consider making it a contribution to Hibernate Validator!

@CNPJ

检查带注释字符序列是否表示巴西公司纳税人注册号 (Cadastro de Pessoa Jurídica)

Checks that the annotated character sequence represents a Brazilian corporate tax payer registry number (Cadastro de Pessoa Jurídica)

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家/地区巴西

CountryBrazil

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

巴西

Brazil

@CPF

检查带注释字符序列是否表示巴西个人纳税人注册号 (Cadastro de Pessoa Física)

Checks that the annotated character sequence represents a Brazilian individual taxpayer registry number (Cadastro de Pessoa Física)

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家/地区巴西

CountryBrazil

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

巴西

Brazil

@TituloEleitoral

检查带注释字符序列是否表示巴西选民身份证号码 ( Título Eleitoral )

Checks that the annotated character sequence represents a Brazilian voter ID card number (Título Eleitoral)

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家/地区巴西

CountryBrazil

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

巴西

Brazil

@NIP

检查带注释字符序列是否表示波兰 VAT 识别号 ( NIP )

Checks that the annotated character sequence represents a Polish VAT identification number (NIP)

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家/地区波兰

CountryPoland

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

波兰

Poland

@PESEL

检查带注释字符序列是否表示波兰国家身份证号码 ( PESEL )

Checks that the annotated character sequence represents a Polish national identification number (PESEL)

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家/地区波兰

CountryPoland

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

波兰

Poland

@REGON

检查带注释字符序列是否代表波兰纳税人身份识别号 ( REGON )。可以应用于 REGON 的 9 位数和 14 位数版本

Checks that the annotated character sequence represents a Polish taxpayer identification number (REGON). Can be applied to both 9 and 14 digits versions of REGON

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家/地区波兰

CountryPoland

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

波兰

Poland

@INN

检查带注释字符序列是否代表俄罗斯纳税人身份识别号 ( INN )。可以应用于 INN 的个人和司法版本

Checks that the annotated character sequence represents a Russian taxpayer identification number (INN). Can be applied to both individual and juridical versions of INN

支持的数据类型_CharSequence_

Supported data types_CharSequence_

Hibernate 元数据影响无

Hibernate metadata impactNone

国家俄罗斯

CountryRussia

Supported data types

CharSequence

CharSequence

Hibernate metadata impact

None

Country

俄罗斯

Russia

在某些情况下 Jakarta Bean Validation 约束或 Hibernate Validator 提供的自定义约束无法满足您的要求。在这种情况下,您可以轻松编写自己的约束。您可以在 Chapter 6, Creating custom constraints 中找到更多信息。

In some cases neither the Jakarta Bean Validation constraints nor the custom constraints provided by Hibernate Validator will fulfill your requirements. In this case you can easily write your own constraint. You can find more information in Chapter 6, Creating custom constraints.