Home > database >  How to create columns in JDL, that only accepts two values, Y or N? Y meaning Yes and N meaning No
How to create columns in JDL, that only accepts two values, Y or N? Y meaning Yes and N meaning No

Time:01-17

entity Platform {
    platformName String required maxlength(100),
    onPermiseFlag String maxlength(1),
    cloudFlag String maxlength(1) 
}

The column 'onPermiseFlag' and 'cloudFlag' can have only two value either 'Y' or 'N'? How to add validation for this?

How to add validations in JDL so that these two columns take only two values either 'Y' or 'N'

CodePudding user response:

you can achieve it using regexp, add the following:

onPermiseFlag(pattern("^[NY]$")),
cloudFlag(pattern("^[NY]$"))

EDIT 1:

entity Platform {
    platformName String required maxlength(100),
    onPermiseFlag String maxlength(1) {
        validate {
            if (onPermiseFlag != "N" && onPermiseFlag != "Y") {
                throw new IllegalArgumentException("WHATEVER YOU WANT")
            }
        }
    },
    cloudFlag String maxlength(1) {
        validate {
            if (cloudFlag != "N" && cloudFlag != "Y") {
                throw new IllegalArgumentException("WHATEVER YOU WANT")
            }
        }
    } 
}

CodePudding user response:

Why not using a boolean in entity and a String in DTO? This is usually a good practice to decouple presentation from storage design.

Anyway, if you want to use String, you could use enums.

https://www.jhipster.tech/jdl/enums

enum OnPremise { Y, N}

enum Cloud { Y, N}

entity Platform {
    platformName String required maxlength(100),
    onPermiseFlag OnPremise,
    cloudFlag Cloud
}

Alternatively, you could use a single enum, this way you could make it evolve if in future you need to add another kind of deployment.

enum Deployment{ ON_PREMISE, CLOUD}

entity Platform {
    platformName String required maxlength(100),
    deployment Deployment
}
  • Related