-
-
Save smyrman/ea15603ec2c4adcf727b4b651184692a to your computer and use it in GitHub Desktop.
Prove that rest-layer/schema accepts empty Validators.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output is: