From c3ec65f2c19619e67586b6ff49ff232f378d351f Mon Sep 17 00:00:00 2001 From: gram Date: Thu, 21 Nov 2024 11:36:57 +0100 Subject: [PATCH] examples --- valdo/example_test.go | 111 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 valdo/example_test.go diff --git a/valdo/example_test.go b/valdo/example_test.go new file mode 100644 index 0000000..e80f4a8 --- /dev/null +++ b/valdo/example_test.go @@ -0,0 +1,111 @@ +package valdo_test + +import ( + "fmt" + + "github.com/orsinium-labs/valdo/valdo" +) + +func ExampleDefaultLocales() { + language := "nl" + original := valdo.Int() + translated := valdo.DefaultLocales.Wrap(language, original) + input := []byte(`"hi"`) + err := valdo.Validate(translated, input) + fmt.Println(err) + // Output: ongeldig type: kreeg string, verwachtte integer +} + +func ExampleLocales() { + language := "nl" + original := valdo.Int() + locales := valdo.Locales{ + "nl": valdo.Locale{ + valdo.ErrType{}: "ongeldig type: kreeg {got}, verwachtte {expected}", + }, + } + translated := locales.Wrap(language, original) + input := []byte(`"hi"`) + err := valdo.Validate(translated, input) + fmt.Println(err) + // Output: ongeldig type: kreeg string, verwachtte integer +} + +func ExampleLocale() { + original := valdo.Int() + locale := valdo.Locale{ + valdo.ErrType{}: "ongeldig type: kreeg {got}, verwachtte {expected}", + } + translated := locale.Wrap(original) + input := []byte(`"hi"`) + err := valdo.Validate(translated, input) + fmt.Println(err) + // Output: ongeldig type: kreeg string, verwachtte integer +} + +func ExampleString() { + validator := valdo.String() + input := []byte(`"hi"`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleInt() { + validator := valdo.Int() + input := []byte(`13`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleBool() { + validator := valdo.Bool() + input := []byte(`true`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleFloat64() { + validator := valdo.Float64() + input := []byte(`3.14`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleArray() { + validator := valdo.Array(valdo.Int()) + input := []byte(`[3, 4, 5]`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleObject() { + validator := valdo.Object( + valdo.P("age", valdo.Int()), + ) + input := []byte(`{"age": 42}`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleValidate() { + validator := valdo.Object( + valdo.P("age", valdo.Int()), + ) + input := []byte(`{"age": 42}`) + err := valdo.Validate(validator, input) + fmt.Println(err) + // Output: +} + +func ExampleSchema() { + validator := valdo.Int() + schema := valdo.Schema(validator) + fmt.Println(string(schema)) + // Output: {"type":"integer"} +}