All Projects → michaeljs1990 → Val

michaeljs1990 / Val

Licence: mit
golang JSON validation library.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Val

Mson React
React and Material-UI Rendering Layer for MSON
Stars: ✭ 74 (+100%)
Mutual labels:  json, validation
Json Schema Spec
The JSON Schema I-D sources
Stars: ✭ 2,441 (+6497.3%)
Mutual labels:  json, validation
Typescript Runtime Type Benchmarks
Benchmark Comparison of Packages with Runtime Validation and TypeScript Support
Stars: ✭ 119 (+221.62%)
Mutual labels:  json, validation
Govalid
Data validation library for golang. [MIGRATING TO NEW ADDRESS]
Stars: ✭ 59 (+59.46%)
Mutual labels:  json, validation
Specs
Technical specifications and guidelines for implementing Frictionless Data.
Stars: ✭ 403 (+989.19%)
Mutual labels:  json, validation
Fhir.js
Node.JS library for serializing/deserializing FHIR resources between JS/JSON and XML using various node.js XML libraries
Stars: ✭ 61 (+64.86%)
Mutual labels:  json, validation
Node Convict
Featureful configuration management library for Node.js
Stars: ✭ 1,855 (+4913.51%)
Mutual labels:  json, validation
Framework
Strongly-typed JavaScript object with support for validation and error handling.
Stars: ✭ 136 (+267.57%)
Mutual labels:  json, validation
Mson
🏗️MSON Lang: Generate an app from JSON
Stars: ✭ 378 (+921.62%)
Mutual labels:  json, validation
Schema
📐 Validating data structures against a given Schema.
Stars: ✭ 359 (+870.27%)
Mutual labels:  json, validation
Schemas
All schemas used for validation that are shared between our projects
Stars: ✭ 51 (+37.84%)
Mutual labels:  json, validation
Winterfell
Generate complex, validated and extendable JSON-based forms in React.
Stars: ✭ 787 (+2027.03%)
Mutual labels:  json, validation
Partial.lenses.validation
Partial Lenses Validation is a JavaScript library for validating and transforming data
Stars: ✭ 39 (+5.41%)
Mutual labels:  json, validation
Schemasafe
A reasonably safe JSON Schema validator with draft-04/06/07/2019-09 support.
Stars: ✭ 67 (+81.08%)
Mutual labels:  json, validation
Validation
validation api extracted from play
Stars: ✭ 194 (+424.32%)
Mutual labels:  json, validation
Encoding
Go package containing implementations of efficient encoding, decoding, and validation APIs.
Stars: ✭ 705 (+1805.41%)
Mutual labels:  json, validation
Cti Stix Validator
OASIS TC Open Repository: Validator for STIX 2.0 JSON normative requirements and best practices
Stars: ✭ 24 (-35.14%)
Mutual labels:  json, validation
Node Quick Mock
🌞 基于Express的mock接口平台
Stars: ✭ 33 (-10.81%)
Mutual labels:  json
Json
Fork of golang's encoding/json: now with more validation and error context
Stars: ✭ 35 (-5.41%)
Mutual labels:  json
Jsontocodable
A generating tool from Raw JSON to Codable (Swift4) text written in Swift4.
Stars: ✭ 33 (-10.81%)
Mutual labels:  json

val

GoDoc Build Status

Go JSON validation library.

This library was developed to meet some validation needs that I needed. However I would like to build this into a much more robust set of tools. Please feel free to ask for any feature or submit a pull request.

Start using it

Run the following in your terminal to start using val.

go get github.com/michaeljs1990/val

Then import it in your Go! code:

import "github.com/michaeljs1990/val"

Update from an old version

Run the following in your terminal to update val to the current master branch.

go get -u github.com/michaeljs1990/val

Example Usage

Basic example

Pointers are needed so go's json.Decode() call can function as one would expect. Pointers are now required in version 0.1 they were not implimented which lead to issues if you wanted json that was not required since int's would be set to 0 even if nothing was passed in causing you to potentially fail a test.

package main

import (
	"fmt"
	"github.com/michaeljs1990/val"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {

	var Register struct {
		Username *string `json:"username" validate:"required"`
		Password *string `json:"password" validate:"required"`
		Email    *string `json:"email" validate:"required|email"`
		Notify   *string `json:"notify" validate:"required|in:yes,no"`
	}

	if err := val.Bind(r.Body, &Register); err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(*Register.Username)
		fmt.Println("This validated!")
	}

}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

Performance

I have created some benchmarks to see what really needs to be improved. Currently the benchmarks run 100,000 times and the performace is as follows. Below shows that email is an expensive call due to the non-optimized regex lib in go. Hopefully this will be improved over time. Other than that I am fairly happy with the current benchmarks. They would most likely be even a bit lower since on an http server you would not have to call a function every iteration to turn a string into a io.ReadCloser.

val general test took: 3.9768205s to run.
val email test took: 3.3563791s to run.
val in test took: 636.4503ms to run.
val required test took: 635.4693ms to run.
val length test took: 693.4902ms to run.
val length between test took: 713.4889ms to run.

To get an idea of where this sits in regards to other validation. In PHP using laravel for this same type of validation it will take you 13+ seconds to run 'val general test' and 10+ seconds to run the required tests for the same number of iterations.

Currently Supported Validation

required

This will ensure that the data is actually included in the json array.

Username *string   `json:"username" validate:"required"`

email

This checks to see if the passed in value is a valid email, it uses the following regex "^[a-zA-Z0-9_.+-][email protected][a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$".

Username *string   `json:"username" validate:"email"`

in

In support any length of arguments to validate a JSON string or int value against.

Username *string   `json:"username" validate:"in:only,these,are,valid,strings"`

min

Min works with ints and ensures that the number the user has entered is not under the specified min. If the number is under it will return an error.

Username *string   `json:"username" validate:"min:10"`

max

Max works with ints and ensures that the number the user has entered is not over the specified max. If the number is over it will return an error.

Username *string   `json:"username" validate:"max:243"`

regex

Regex ensures that the string the user has passed in matched the regex you have entered. Currently this is only tested with strings.

Username *string   `json:"username" validate:"regex:\\d+"`

length

Length ensures that the passed in string is equal to the length you have specified.

Username *string   `json:"username" validate:"length:5"`

length_between

Length between works much the same as length except it will return true if the number is equal to or inbetween the high and low bounds set.

Username *string   `json:"username" validate:"length_between:2,5"`

combinations

If you would like to ensure multiple conditions are met simply use the | character.

Username *string   `json:"username" validate:"email|required|in:[email protected],[email protected]"`
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].