jsonvalidator

package module
v0.0.0-...-2f127dc Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2021 License: MIT Imports: 6 Imported by: 0

README

jsonvalidator

A json validator based on json template which supports go-playground/validator and regular expression.

Installation

go get github.com/seaguest/jsonvalidator

Usage

The template json value is composed by type|rule. for example:

all supported type:

tokenInt       = "{int}"
tokenFloat     = "{float}"
tokenString    = "{string}"
tokenList      = "{list}"
tokenRe        = "{re}" // regular expression
tokenSeparator = "|"

{string}|eq=ok    declare the node as string and equal ok
{int}|eq=0        declare the node as int and equal 0
{re}|^0$          declare validation type as regular expression
package jsonvalidator

import (
	"fmt"
	"testing"
)

const tpljson = `
{
  "int": "{int}|eq=0",
  "string": "{string}|eq=0",
  "re": "{re}|^123456$",
  "list1": "{list}|eq=2",
  "list2": [
    "{int}|eq=1",
    "{int}|eq=2"
  ],
  "data": {
    "token": "{re}|Bearer\\s{1}",
    "password": "{re}|^123456$"
  }
}
`

const srcjson = `
{
  "int": 0,
  "string": "0",
  "re": "123456",
  "list1": [
    1,
    2
  ],
  "list2": [
    1,
    2
  ],
  "ts": "19226715",
  "data": {
    "token": "Bearer 1|ssds",
    "password": "123456",
    "ok": "yes"
  }
}
`

func TestValidate(t *testing.T) {
	err := ValidateJson(srcjson, tpljson)
	fmt.Println(err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ValidateJson

func ValidateJson(input, tpl string) (err error)

func ValidateObject

func ValidateObject(input, tpl interface{}) (err error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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