kcl-lang / kcl

KCL Programming Language (CNCF Sandbox Project). https://kcl-lang.io
https://kcl-lang.io
Apache License 2.0
1.41k stars 110 forks source link

How to check the optional attribute #1443

Closed LinYunling closed 1 week ago

LinYunling commented 1 week ago

Code:

schema testModel:
    name: str
    version?: str

    check:
        regex.match(name, r"^[a-z_]{1,62}$")
        regex.match(version, r"^\d{1,3}.\d{1,3}.\d{1,3}$")

test = testModel {
    name = "abc"
    #version = "1.0.0"
}

kcl run, report the error: image

if I define the verison in test, everything is ok So how to check the optional attribute thanks

Peefy commented 1 week ago

You can use the check if filter.

import regex

schema testModel:
    name: str
    version?: str

    check:
        regex.match(name, r"^[a-z_]{1,62}$")
        regex.match(version, r"^\d{1,3}.\d{1,3}.\d{1,3}$") if version

test = testModel {
    name = "abc"
    version = "1.0.0"
}
LinYunling commented 1 week ago

thanks