[XCORE] how to implement validation [message #1253803] |
Sat, 22 February 2014 13:18  |
Eclipse User |
|
|
|
Since I'm just starting out with emf and xcore I was wondering how to validate my model and add custom validation rules. For example how do I specify that a string field must not be empty and must contain at least 2 characters or that a numeric field must be in the range -127 to 128 ?
Would it be possible to configure the validation rules at runtime ?
Is the EMF validation framework the right place to look ? At first sight I would have expected some declarative approach to specify the constraints.
[Updated on: Sat, 22 February 2014 13:34] by Moderator
|
|
|
|
|
Re: [XCORE] how to implement validation [message #1256761 is a reply to message #1256720] |
Tue, 25 February 2014 14:31   |
Eclipse User |
|
|
|
Roger,
Comments below.
On 25/02/2014 7:35 PM, Roger Gilliar wrote:
> I can still can figure out how to implement validation.
> I tried:
>
> @ExtendedMetaData(name="moduleNo",
> baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
> minInclusive="0",
> maxInclusive="65534")
> unique Integer moduleNo
>
> That gives me a comment at the interface but no code seems to be
> generated that does the check.
That's because it's all done in EObjectValidator based on the
annotations in the Ecore model.
Note however that I mentioned that such annotations are on a classifier,
but above you show it on a structural feature. I.e., it should be like this
@ExtendedMetaData
(baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
minInclusive="0",
maxInclusive="65534")
type PositiveInt wraps int
class Example
{
PositiveInt feature
}
Think of it like in XML Schema, i.e., you define a new simple type with
facet constraints and then use that type for elements or attributes.
> I think I'm totally misunderstanding how validation is supposed to
> work in an EMF context. With Bean validation I would write something
> like
>
> class Test {
> @Min(0)
> @Max(65534)
> Integer moduleNo;
> }
>
> validator.validate(testObject);
Diagnostician.INSTANCE.validate(eObject) does the trick.
>
> But I have no idea how to do this with EMF.
|
|
|
|
Re: [XCORE] how to implement validation [message #1257151 is a reply to message #1256787] |
Wed, 26 February 2014 00:43  |
Eclipse User |
|
|
|
Roger,
Comments below.
On 25/02/2014 9:14 PM, Roger Gilliar wrote:
> Cool,
>
> it's working now.
> BTW:
> The following code causes an error:
>
> @ExtendedMetaData
> (baseType="http://www.eclipse.org/emf/2003/XMLType#integer",
> ..
>
> but the following works:
> (baseType="http://www.eclipse.org/emf/2003/XMLType#Integer",
Sorry, I should have looked at the generated code to see it doesn't
compile.
>
> The first one tries to create a BigInteger and the generated source
> does not compile.
Yes you have to make sure that the wrapped type of what you extend and
the wrapped type you define match. You can leave out the baseType as
well; that annotation just ensures that you "inherit" the constraints of
the specified base type.
|
|
|
Powered by
FUDForum. Page generated in 0.04157 seconds