Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Latest commit

 

History

History
40 lines (28 loc) · 1.78 KB

README.md

File metadata and controls

40 lines (28 loc) · 1.78 KB

Val

Val is a theoretical/experimental validation library written in Go. Its primary inteded purpose is validation of user-input data (usually from web forms). It should not (yet) be used in a production context.

Working with Contracts

A contract, in val-speak, is a list of terms ([]Conditions) which must be met. To create a contract, do:

var contract = val.NewContract()

Add rules to a contract with contract.AddRule(*val.Data, val.ValidatorFunc). In the following example val.EqualsString is a built-in validation function which validates that the value contained in the provided data equals the string provided in its first parameter.

var data = NewData([]byte("value"), "DATA-TYPE", "DATA-CATEGORY")
contract.AddRule(data, val.EqualsString("value", "Values are not equal."))

To run validation, call contract.Validate(). The returned boolean indicates whether the contract passed all rules (true) or failed one or more rule (false).

When a contract fails validation, pointers to each failed Condition are provided in contract.Fails.

if ok := contract.Validate(); !ok {
	for _, f := range contract.Fails {
		fmt.Printf("Expected rules to pass; Data with sig `%v` returned errors: %v",
			f.Condition.Data.Signature,
			f.Error)
	}
}

Contributing

Pull-requests are welcome, and so are you. :)

Currently, approved PRs are those which improve performance and documentation, add useful functionality (core lib or built-ins), or fix bugs / flaws.

If you'd like to submit a PR which changes the API, adds currently unrepresented functionality, or modifies existing (stable) behavior, create an issue and reference your pull request so we can talk about it.

These contribution guidelines may be more structured in the future, depending on community reception.