Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dedalqq/omg.jsonparser
The simple JSON parser with validation by condition
https://github.com/dedalqq/omg.jsonparser
go golang json-parsing validation
Last synced: about 2 months ago
JSON representation
The simple JSON parser with validation by condition
- Host: GitHub
- URL: https://github.com/dedalqq/omg.jsonparser
- Owner: dedalqq
- License: mit
- Created: 2021-07-08T23:59:21.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-10-12T12:34:19.000Z (about 3 years ago)
- Last Synced: 2024-07-31T20:52:11.323Z (4 months ago)
- Topics: go, golang, json-parsing, validation
- Language: Go
- Homepage:
- Size: 32.2 KB
- Stars: 6
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go - omg.jsonparser - Simple JSON parser with validation by condition via golang struct fields tags. (JSON / Search and Analytic Databases)
- awesome-ccamel - dedalqq/omg.jsonparser - The simple JSON parser with validation by condition (Go)
- awesome-go-extra - omg.jsonparser - 07-08T23:59:21Z|2021-10-12T12:34:19Z| (JSON / Advanced Console UIs)
README
# omg.jsonParser
[![Go](https://github.com/dedalqq/omg.jsonparser/actions/workflows/go.yml/badge.svg)](https://github.com/dedalqq/omg.jsonparser/actions/workflows/go.yml)
[![Go Reference](https://pkg.go.dev/badge/github.com/dedalqq/omg.jsonparser.svg)](https://pkg.go.dev/github.com/dedalqq/omg.jsonparser)
[![Coverage Status](https://coveralls.io/repos/github/dedalqq/omg.jsonparser/badge.svg?branch=master)](https://coveralls.io/github/dedalqq/omg.jsonparser?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/dedalqq/omg.jsonparser)](https://goreportcard.com/report/github.com/dedalqq/omg.jsonparser)omg.jsonParser is a simple JSON parser with a simple condition validation. It's a wrapper on standard go JSON lib. With it help you can add the validation condition via golang structure fields tags.
## Example
```go
package mainimport (
"fmt""github.com/dedalqq/omg.jsonparser"
)func main() {
jsonData := `{"name": ""}`st := struct {
Name string `json:"name,notEmpty"` // added notEmpty for enable validation for it field
}{}err := jsonparser.Unmarshal([]byte(jsonData), &st)
if err != nil {
fmt.Println(err.Error()) // print: value [name] must be not empty
}
}```
## Tag struct
```
json:"[name][,option]..."
```### Tag examples
Here are some the tag examples:
```go
package maintype data struct {
F1 string `json:"f1,notEmpty"` // the field must be not empty but can be "null" or may not exist
F2 string `json:"f2,notEmpty,required"` // the field is required and must be not empty but may be the "null" value
F3 string `json:"f3,notEmpty,notNull"` // the field must be not empty and not "null" but may not exist
F4 []string `json:"f4,notNull,min:3"` // the field must be not "null" and contains 3 or more items but may not exist
}
```## Available tags options
| Name | Types | Description |
| ----------- | -------- | -------------------------------------------------------------------------------------- |
| `required` | any | The field must be exist with any value or `null` |
| `notEmpty` | any | The field can be not exist but if exist value must be not zero value but can be `null` |
| `notEmpty` | slice | The field must have one element or more but may be `null` or not exist |
| `notNull` | any | The field should not be null, but may not exist |
| `uniq` | []string | The strings slice must contains only unique strings |
| `min:n` | slice | The slice must have `n` items or more |
| `max:n` | slice | The slice must have `n` items or less |
| `min:n` | string | The string must have `n` runes or more |
| `max:n` | string | The string must have `n` runes or less |
| `min:n` | int/uint | The value must be equals `n` or more |
| `max:n` | int/uint | The value must be equals `n` or less |