examples/

directory
v2.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2024 License: MIT

README

Examples

Three examples showing some useage examples of the library.

  • cli - validating flag input in a command line application
  • http-inline - simple stdlib http server, showing request validation within a handler
  • http-validate - sample of using an error handler combined with the validation.Validator interface

To test them, just run go run main.go targeting the relevant directory.

For the http servers, an example valid POST request is:

POST http://localhost:1234/

{
    "name":"My Name",
    "dob":"2000-10-12T07:20:50.52Z",
    "count":1,
    "isEnabled":false
}

An invalid request:

POST http://localhost:1234/

{
    "name":"My Name",
    "dob":"2000-10-12T07:20:50.52Z",
    "count":0,
    "isEnabled":true
}

The invalid request will then return this response with a 400 status code:

{
    "errors": {
        "count": [
            "value 0 should be greater than 0"
        ],
        "isEnabled": [
            "value true does not evaluate to false"
        ]
    }
}

Directories

Path Synopsis
A simple stdlib http handler that validates a request inline - rather than using the validator.Validator interface.
A simple stdlib http handler that validates a request inline - rather than using the validator.Validator interface.
This has the same logic as the http-inline example but it has implemented validator.Validator and as such requests can be checked in a single place.
This has the same logic as the http-inline example but it has implemented validator.Validator and as such requests can be checked in a single place.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL