Skip to content

daichitakahashi/rego-validation-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rego-validation-example

Examples for validation using Policy Language Rego.

Policies

  • policy/email.rego: validate email address
    • policy/email_test.rego: test code
  • policy/domain.rego: validate domain part of email address
    • policy/domain_test.rego: test code
  • artifact/policy.wasm: bundled WebAssembly(make build)

Examples

Go

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(node.js)

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(3.10)

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}