Examples for validation using Policy Language Rego.
policy/email.rego
: validate email addresspolicy/email_test.rego
: test code
policy/domain.rego
: validate domain part of email addresspolicy/domain_test.rego
: test code
artifact/policy.wasm
: bundled WebAssembly(make build
)
Go example handles ".rego" files directly using github.com/open-policy-agent/opa.
$ cd go
$ go test -v .
=== RUN TestValidEmail
=== RUN TestValidEmail/[email protected]
=== RUN TestValidEmail/hoge@[email protected]
=== RUN TestValidEmail/hoge@example
--- PASS: TestValidEmail (0.00s)
--- PASS: TestValidEmail/[email protected] (0.00s)
--- PASS: TestValidEmail/hoge@[email protected] (0.00s)
--- PASS: TestValidEmail/hoge@example (0.00s)
=== RUN TestValidDomain
=== RUN TestValidDomain/example.com
=== RUN TestValidDomain/example
--- PASS: TestValidDomain (0.00s)
--- PASS: TestValidDomain/example.com (0.00s)
--- PASS: TestValidDomain/example (0.00s)
PASS
ok rego-validation-example/go 0.365s
Javascript example uses "policy.wasm" with @open-policy-agent/opa-wasm.
$ cd node
$ npm install
$ node test.mjs
[validation/email]
[email protected] { valid: true }
hogehogeexample.com { valid: false }
hoge@[email protected] { valid: false }
hogehoge@examplecom { valid: false }
[validation/domain]
example.com { valid: true }
examplecom { valid: false }
.example.com { valid: false }
Python example uses "policy.wasm" too, with opa-wasm.
$ cd python
$ pip3 install -r requirements.txt
$ python3 test.py
[validation/email]
[email protected] {'valid': True}
hogehogeexample.com {'valid': False}
hoge@[email protected] {'valid': False}
hogehoge@examplecom {'valid': False}
[validation/domain]
example.com {'valid': True}
examplecom {'valid': False}
.example.com {'valid': False}