=== Declarative validation

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
:page-partial:


The xref:refguide:applib:index/annotation/Parameter.adoc#mustSatisfy[mustSatisfy()] element allows arbitrary validation to be applied to parameters using an (implementation of a) xref:refguide:applib:index/spec/Specification.adoc[Specification] object.

[TIP]
====
The specification implementations can (of course) be reused between parameters and xref:refguide:applib:index/annotation/Property.adoc#mustSatisfy[properties].
====

The `Specification` is consulted during validation, being passed the proposed value.
If the proposed value fails, then the value returned is the used as the invalidity reason.

For example:

[source,java]
.StartWithCapitalLetterSpecification.java
----
public class StartWithCapitalLetterSpecification
        extends AbstractSpecification<String> {            // <.>

    public String satisfiesSafely(String proposed) {
        return "".equals(proposed)
            ? "Empty string"
            : !Character.isUpperCase(proposed.charAt(0))
                ? "Does not start with a capital letter"
                : null;

    }
}
----
<.> the `AbstractSpecification` class conveniently handles type-safety and dealing with null values.
The applib also provides `SpecificationAnd` and `SpecificationOr` to allow specifications to be combined "algebraically".

can then be used:

[source,java]
.CustomerRepository.java
----
public class CustomerRepository {
    public Customer newCustomer(
                @Parameter(
                    mustSatisfy=StartWithCapitalLetterSpecification.class
                )
                final String firstName,
                @Parameter(
                    mustSatisfy=StartWithCapitalLetterSpecification.class
                )
                final String lastName) {
        // ...
    }
    ...
}
----

==== i18n

It is also possible to provide translatable reasons.
Rather than implement `Specification`, instead implement xref:applib:index/spec/Specification2.adoc[Specification2].
This defines the API:

[source,java]
----
public interface Specification2 extends Specification {
    public TranslatableString satisfiesTranslatable(Object obj); // <.>
}
----
<.> Return `null` if specification satisfied, otherwise the reason as a translatable string

With `Specification2` there is no need to implement the inherited `satifies(Object)`; that method will never be called.



