Govalid is a data validation library that can validate most data types supported by golang

Overview

Govalid godoc Build Status Coverage Status License: Unlicense

Govalid is a data validation library that can validate most data types supported by golang. Custom validators can be used where the supplied ones are not enough.

import v "github.com/gima/govalid/v1"

Example

Create a validator:

schema := v.Object(
	v.ObjKV("status", v.Boolean()),
	v.ObjKV("data", v.Object(
		v.ObjKV("token", v.Function(myValidatorFunc)),
		v.ObjKV("debug", v.Number(v.NumMin(1), v.NumMax(99999))),
		v.ObjKV("items", v.Array(v.ArrEach(v.Object(
			v.ObjKV("url", v.String(v.StrMin(1))),
			v.ObjKV("comment", v.Optional(v.String())),
		)))),
		v.ObjKV("ghost", v.Optional(v.String())),
		v.ObjKV("ghost2", v.Optional(v.String())),
		v.ObjKV("meta", v.Object(
			v.ObjKeys(v.String()),
			v.ObjValues(v.Or(v.Number(v.NumMin(.01), v.NumMax(1.1)), v.String())),
		)),
	)),
)

Validate some data using the created validator:

if path, err := schema.Validate(data); err == nil {
	t.Log("Validation passed.")
} else {
	t.Fatalf("Validation failed at %s. Error (%s)", path, err)
}
// Example of failed validation:

// Validation failed at Object->Key[data].Value->Object->Key[debug].Value->Number.
// Error (expected (*)data convertible to float64, got bool)

You can also take a look at the "tests/" folder. (Sorry, but if you feel more documentation is needed, please open an issue.)

Similar libraries

Go check
Javascript js-schema, jsonvalidator
Python voluptuous, json_schema
Ruby json-schema

Original idea for jsonv (version 0 of this library, before rename) loosely based on js-schema, thank you.

License

http://unlicense.org
Authoritative: UNLICENSE.txt
Mention of origin would be appreciated.

jsonv, jsonv2, json validator, json validation, alternative, go, golang

You might also like...
With Pasteback you can store common terms and paste them back to clipboard

Pasteback With Pasteback you can store common terms and paste them back to clipboard when needed. There is one central spot to put every string into y

ms - 'my story' creates a secure password string which can be memorized with a technique shared by Max.

On 23.12.21 20:22, Stefan Claas wrote: [...] Yes, I am aware of that, but how can one memorize a key when traveling and not taking any devices

Otx - otx tool can scrap to find sensitive information and vulnerable endpoint urls.
Otx - otx tool can scrap to find sensitive information and vulnerable endpoint urls.

otx Description This tool is base on AlienVault Open Threat Exchange (OTX)? and this tool can help you to extract all the urls endpoints which can be

This utility can be used for converting BSE Mutual fund scheme

go-mf-scheme-master-generator This utility can be used for converting BSE Mutual

JIN the coolest fighting game ever made that uses the M.U.G.E.N engine so heres how you can build it

JIN the coolest fighting game ever made that uses the M.U.G.E.N engine so heres how you can build it

This library provides an ASTERIX Frame(binary data) decoding/parsing(json,xml) capabilities for Go.

GoAsterix This library provides an ASTERIX Frame(binary data) decoding/parsing(json,xml) capabilities for Go. ASTERIX ASTERIX (All Purpose Structured

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.
Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package.

Goridge is high performance PHP-to-Golang codec library which works over native PHP sockets and Golang net/rpc package. The library allows you to call Go service methods from PHP with a minimal footprint, structures and []byte support.

encLib is a simple golang package for quickly encoding and decoding string data in hex

encLib is a simple golang package for quickly encoding and decoding string data in hex

Golang Fake data generator

Fake data generator. Written in Go Installation Faker requires Go 1.17 go get

Comments
  • Nil Validator

    Nil Validator

    Have you ever considered adding a nil validator to this tool? I was trying to use this a little earlier to test a JSON file that required a null in one of the key-value pairs but couldn't work out how to do this with govalid.

    I've since forked this repo and added a function that checks for null (I've added a set of tests for this as well) so I could send a PR if you think that this is a good idea.

    opened by kohrVid 3
  • Marshaling validators

    Marshaling validators

    Hi,

    At $work we have a multi-tenant API using JWT authentication.It would be great if govalid implemented the Marshaler interface, as we then could encode validators in the API access token and implement API key-specific validation policies.

    opened by brknstrngz 2
  • How to validate mandatory and/or optional keys in JSON?

    How to validate mandatory and/or optional keys in JSON?

    I have a JSON object(fixed schema) where few keys are mandatory and few are optional. When I define a validator(non-optional) for a key, it checks for presence of the key and then performs validation on value of the key. e.g. v.ObjKV("url", v.String()), in this case validator checks for "url" key in JSON. If it is not present, throws and error, Key[url].Value->String . Error expected (*)string, was When I define a optional validator for a key, it does not perform validation if key is not present and returns success. e.g. v.ObjKV("url", v.Optional(v.String())), in this case validator returns success even if "url" key is not present in JSON. Based on this, I have two questions:

    1. How to validate keys which are optional in JSON?
    2. Do we have support for fixed schema where validator will only check for keys mentioned in the schema?
    opened by wiredmb 2
The one-stop shop for most common Go functions

Pandati The one stop shop for most common Go functions Table of contents Pandati The one stop shop for most common Go functions Table of contents Purp

Lukasz Raczylo 2 Mar 21, 2022
Golang code-generators used to implement Kubernetes-style API types.

code-generator Golang code-generators used to implement Kubernetes-style API types. Purpose These code-generators can be used in the context of Custom

Kubernetes 1.3k Dec 30, 2022
Slice conversion between primitive types

sliceconv Sliceconv implements conversions to and from string representations of primitive types on entire slices. The package supports types int, flo

Henry Sarabia 8 Sep 27, 2022
Some utility functions for generic types in Go.

GOUF - Utility Functions for generic types Go team released Go 1.18 beta recently with support for Generics(a.k.a type parameters). This package provi

Akshay Bharambe 18 Apr 13, 2022
GoDynamic can load and run Golang dynamic library compiled by -buildmode=shared -linkshared

GoDynamic can load and run Golang dynamic library compiled by -buildmode=shared -linkshared How does it work? GoDynamic works like a dynamic

pkujhd 11 Sep 30, 2022
libraries for various programming languages that make it easy to generate per-process trace files that can be loaded into chrome://tracing

chrometracing: chrome://tracing trace_event files The chrometracing directory contains libraries for various programming languages that make it easy t

Google 23 Oct 6, 2022
流媒体NetFlix解锁检测脚本 / A script used to determine whether your network can watch native Netflix movies or not

netflix-verify 流媒体NetFlix解锁检测脚本,使用Go语言编写 在VPS网络正常的情况下,哪怕是双栈网络也可在几秒内快速完成IPv4/IPv6的解锁判断 鸣谢 感谢 @CoiaPrant 指出对于地域检测更简便的方法 感谢 @XmJwit 解决了IPV6 Only VPS无法下载脚

null 2k Dec 29, 2022
LogAnalyzer - Analyze logs with custom regex patterns.Can search for particular patterns on multiple files in a directory.

LogAnalyzer Analyze logs with custom regex patterns.Can search for particular patterns on multiple files in a directory

Johnson Simon 6 May 31, 2022
Clean-Swift source and test code auto-generator. It can save you time typing 500-600 lines of code.

Clean-Swift source & test code auto generator Overview Run Output Basic Usage make config.yaml target_project_name: Miro // target project name copyri

David Ha 20 Apr 13, 2022
A fully Go userland with Linux bootloaders! u-root can create a one-binary root file system (initramfs) containing a busybox-like set of tools written in Go.

u-root Description u-root embodies four different projects. Go versions of many standard Linux tools, such as ls, cp, or shutdown. See cmds/core for m

null 2k Dec 29, 2022