Check if a string is a number

================
|    Person    |
|--------------|
|- id : String |
|--------------|
================

      

I have a class Person

with a property id

, that is, a type String

. I have to check that id

is a number with 11 digits. I am thinking of something similar:

context Person::id:String
inv:    self.id->forAll(l|l.oclIsTypeOf(Integer))
        and
        self.id.size() = 11 

      

but I feel like it is wrong.

EDIT.

Now I'm pretty sure this is not correct, l.oclIsTypeOf(Integer)

always returns false

because it oclIsTypeOf

should only be called OclAny

when id

is a type String

.

EDIT 2. (Solution)

I solved it like this:

context Person::id:String
inv:    not self.id.toInteger().oclIsInvalid()
        and
        self.id.size() = 11 

      

The solution provided Vincent Aranega

below should work too

+3


source to share


1 answer


There are String

not many methods, but toInteger

you can help there. It returns a value Integer

for String

or Invalid

if the string cannot be converted to Integer

. So:

context Person::id:String
inv:    not self.id.toInteger().oclIsUndefined() 
        and self.id.size() = 11

      



must do the trick! (successfully tested in Acceleo)

+3


source







All Articles