section:TutorialValidation[Creating Custom Validation Rules]

One of the main advantages of DSLs is the possibility to statically validate domain specific constraints.
This can be achieved by means of static analysis. Because this is a common use case,
Xtext provides a dedicated hook for this kind of validation rules. In this lesson, we want to ensure
that the name of an e[Entity] starts with an upper-case letter and that all features have distinct names
across the inheritance relationship of an e[Entity].

Try to locate the class e[DomainmodelJavaValidator] in the package e[org.eclipse.xtext.example.validation].
It can be found in the language plug-in. Defining the constraint itself is only a matter of a few lines
of code:

code[Java][
@Check
public void checkNameStartsWithCapital(Entity entity) {
	if (!Character.isUpperCase(entity.getName().charAt(0))) {
		warning("Name should start with a capital", 
			DomainmodelPackage.Literals.TYPE__NAME);
	}
}
]

Any name for the method will do. The important thing is the codeRef[org.eclipse.xtext.validation.Check]
annotation that advises the framework to use the method as a validation rule. If the name starts with
a lower case letter, a warning will be attached to the name of the e[Entity].

The second validation rule is straight-forward, too. We traverse the inheritance hierarchy of the e[Entity]
and look for features with equal names.

code[Java][
@Check
public void checkFeatureNameIsUnique(Feature f) {
	Entity superEntity = ((Entity) f.eContainer()).getSuperType();
	while(superEntity != null) {
		for(Feature other: superEntity.getFeatures()) {
			if (f.getName().equals(other.getName())) {
				error("Feature names have to be unique",
						DomainmodelPackage.Literals.FEATURE__NAME);
				return;
			}
		}
		superEntity = superEntity.getSuperType();
	}
}
]

The sibling features, that are defined in the same entity, are automatically validated by the Xtext framework.
Therefore, they do not have to be checked twice.

