=== 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:applib:index/annotation/Property.adoc#mustSatisfy[mustSatisfy()] element allows arbitrary validation to be applied to properties using an (implementation of a) `org.apache.isis.applib.spec.Specification` object.
The attribute is also supported on xref:refguide:applib:index/annotation/Parameter.adoc#mustSatisfy[parameters].

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

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]
----
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.

where:

[source,java]
----
import lombok.Getter;
import lombok.Setter;

public class Customer {

    @Property(mustSatisfy=StartWithCapitalLetterSpecification.class)
    @Getter @Setter
    private String firstName;

    // ...
}
----

It is also possible to provide translatable reasons.
Rather than implement `Specification`, instead implement `Specification2` which defines the API:

[source,java]
----
public interface Specification2 extends Specification {
    public TranslatableString satisfiesTranslatable(Object obj); // <1>
}
----
<1> 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.




