[XCORE] how to implement validation [message #1253803] |
Sat, 22 February 2014 18:18 |
Roger Gilliar Messages: 40 Registered: March 2010 |
Member |
|
|
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 18:34] Report message to a moderator
|
|
|
|
|
Re: [XCORE] how to implement validation [message #1256761 is a reply to message #1256720] |
Tue, 25 February 2014 19:31 |
Ed Merks Messages: 33140 Registered: July 2009 |
Senior Member |
|
|
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.
Ed Merks
Professional Support: https://www.macromodeling.com/
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.02976 seconds