Validate a JSON value from a library consumer
This example validates JSON against a Smithy shape from a MoonBit program. The program uses only the published moonrockz/smithy and moonrockz/smithy-validation modules. It does not use the Smithy workspace.
main.mbtx does four things:
It loads a model with
@smithy.Loaderand assembles it. The model hasinteger Countwith@range(min: 1, max: 100)andstructure Input { @required count: Count }.It prepares a validator for
example#Input.It validates
{"count":2}. The value is valid.It validates
{"count":0}. The value is not valid, and the first finding has the codeRange.
Run it from the repository root:
mise run test:validation-consumerThe task copies the program into an isolated module. It resolves the Smithy modules from local package archives. It then runs the program on the native, wasm, js and wasm-gc targets. It checks that the program prints this line:
valid=true invalid=Range at /countThe task also runs rpc-consumer.
To run one target, name it after the script:
mise exec -- moonx scripts/test-validation-consumer.mbtx nativeThe expected output is:
Isolated validation-consumer passed: native
Audited 8 resolved manifests within the consumer's dependency profile
Isolated rpc-consumer passed: native
Audited 8 resolved manifests within the consumer's dependency profileThe script exits with status 0 when every check passes. It exits with a non-zero status when one check fails. The Audited line shows that the program depends on the model and validation modules only.