Skip to content

An eDSL approach to validate whether the target Object matches the definition

Notifications You must be signed in to change notification settings

YSMull/Mona-JSONChecker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

quick start

prepare data

Object data = Map.of(
    "reports", List.of(
        Map.of(
            "id", 1,
            "title", "title",
            "relatedDataModelIds", List.of(1, 2, 3)
        )
    ),
    "dataModels", List.of(
        Map.of(
            "status", "extract",
            "relatedDataConnectionIds", List.of(1, 2, "123")
        )
    )
);

Define your Checker by compose the primitive checker

Chkr checker = Mixin(
    Obj(
        "reports", Arr(Obj(
            "id", Num,
            "title", Str,
            "relatedDataModelIds", Arr(Num)
        ))
    ),
    Or(
        Obj(
            "dataModels", Arr(Obj(
                "status", OrVal("extract", "direct"),
                "relatedDataConnectionIds", Arr(StrictNum)
            ))
        ),
        Obj(
            "dataModels", Arr(Obj(
                "status", OrVal("extract", "direct"),
                "relatedTableExtractIds", Arr(Num)
            ))
        )
    )
);

and run the check:

Object after = checker.check(data);
System.out.println(JSON.toJSONString(after, true));

If the data dosen't match your checker , exception messages will show the mistake precisely~

primitive checker

ChkrName Description
Null target field shoud be null
Any target field shoud not be null
Num target field shoud like Number, for example: 123, "123"
StrictNum target field shoud be Number
Str target field shoud be String
Bool target field shoud like Boolean, for example: true , flase , "True", "true", "fAlsE"
StrictBool target field shoud be Boolean
Mixin
Or
Obj
Arr
Optional
OrVal

About

An eDSL approach to validate whether the target Object matches the definition

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages