Skip to content

Instantly share code, notes, and snippets.

@smyrman

smyrman/main.go Secret

Created January 1, 2017 23:37
Show Gist options
  • Save smyrman/ea15603ec2c4adcf727b4b651184692a to your computer and use it in GitHub Desktop.
Save smyrman/ea15603ec2c4adcf727b4b651184692a to your computer and use it in GitHub Desktop.
Prove that rest-layer/schema accepts empty Validators.
package main
import (
"context"
"fmt"
"os"
"github.com/rs/rest-layer/schema"
)
var taskSchema = &schema.Schema{
Fields: schema.Fields{
"id": schema.IDField,
"task": {
Description: "A valid task name",
Validator: &schema.String{MinLen: 3},
Required: true,
},
"priority": {
Description: "The queue priority of the task, 0 is highest",
Default: 9,
Validator: &schema.Integer{
Boundaries: &schema.Boundaries{Min: 0, Max: 10},
},
},
"parameters": {
Description: "valid parameters depend on the selected task, and are validated when the task starts",
},
},
}
func main() {
if err := taskSchema.Compile(); err != nil {
fmt.Println("ERROR:", err.Error())
os.Exit(1)
}
task1 := map[string]interface{}{
"task": "factorial",
"priority": 2,
"parameters": 5,
}
task2 := map[string]interface{}{
"task": "sortDict",
"priority": 2,
"parameters": map[string]interface{}{"three": 3, "two": 2, "five": 5},
}
c1, b1 := taskSchema.Prepare(context.TODO(), task1, nil, false)
fmt.Println("task 1 base:", b1)
fmt.Println("task 1 change:", c1)
vt1, errs := taskSchema.Validate(c1, b1)
if len(errs) > 0 {
fmt.Println("ERROR:", "task 1 validate", errs)
os.Exit(1)
}
fmt.Println("Validated task 1:", vt1)
c2, b2 := taskSchema.Prepare(context.TODO(), task2, nil, false)
fmt.Println("task 2 base:", b2)
fmt.Println("task 2 change:", c2)
vt2, errs := taskSchema.Validate(c2, b2)
if len(errs) > 0 {
fmt.Println("ERROR:", "task 2 validate", errs)
os.Exit(1)
}
fmt.Println("Validated task 2:", vt2)
}
@smyrman
Copy link
Author

smyrman commented Jan 1, 2017

Output is:

% go run main.go                                                                                                     :(
task 1 base: map[id:b1kp5ntcsrv25cu5khpg]
task 1 change: map[priority:2 parameters:5 task:factorial]
Validated task 1: map[priority:2 parameters:5 id:b1kp5ntcsrv25cu5khpg task:factorial]
task 2 base: map[id:b1kp5ntcsrv25cu5khq0]
task 2 change: map[task:sortDict priority:2 parameters:map[five:5 three:3 two:2]]
Validated task 2: map[priority:2 parameters:map[three:3 two:2 five:5] id:b1kp5ntcsrv25cu5khq0 task:sortDict]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment