package validate

  1. Overview
  2. Docs
OCaml library enabling efficient data validation through PPX derivers and a suite of annotation-based validators

Install

Dune Dependency

Authors

Maintainers

Sources

validate-0.1.0.tbz
sha256=7b3e8978f003d5af74b182c476bbf829572000dd53e7d72ed98cecd4c4b26daf
sha512=ce9f0be079fff7a9250756384954e36e6b12376b5b1b7a75d7c88a14eb323758048faa906a47c7e36ed65854014c549457be6f4f4a6d9cce6be7c52f108b354c

Description

Validate is an OCaml library that focuses on data validation using PPX derivers and a range of annotations for different data types. It allows developers to apply annotations for various validation rules, such as string length, numeric values, and format constraints like URLs and UUIDs. This functionality makes it suitable for a wide array of applications in OCaml development where data integrity is crucial.

Tags

validation ppx

Published: 11 Jan 2024

README

Validate

Note: This is a preview version of the validate library. It is still under development, and users may encounter errors. Feedback and contributions are highly appreciated during this stage.

Overview

validate is an OCaml library designed to streamline the process of validating records, lists, or values. It primarily operates through a PPX deriver that automatically generates validators using annotations, utilizing an underlying library of helper validation functions.

Installation

Installing validate

To install the validate library, use OPAM with the following command:

opam install validate

After installation, you need to add validate as a library in your project's dune file and specify validate.ppx_derive_validate as a preprocessor. Here is an example of how to set up the dune file:

(library
  (name something)
  (preprocess (pps validate.ppx_derive_validate))
  (libraries validate))

Annotations and Usage

The validate library in OCaml allows for precise validation of data through a variety of annotations. For each type, the library generates a function named validate_[type_name] which can be used to perform the validation.

Example Usage

type test_record = {
  min : string; [@min_length 2]
  max : string; [@max_length 5]
  numeric_list : int list; [@list_min_length 2] [@less_than 10]
  ...
}
[@@deriving validate, show, eq]

let example_record = { min = "ab"; max = "hello"; numeric_list = [1, 2, 3] }
let validation_result = validate_test_record example_record

In this example:

  • min is a string field validated for a minimum length of 2.

  • max is validated for a maximum length of 5.

  • numeric_list is an integer list, validated for a minimum length of 2, and each element is validated to be less than 10.

Categorized Annotations

String Annotations

  • @min_length: Validates minimum length of a string.

  • @max_length: Validates maximum length of a string.

  • @url: Checks if a string is a valid URL.

  • @uuid: Validates a string as a UUID.

  • @numeric: Ensures a string contains only numeric characters.

  • @alpha: Checks for alphabetic characters in a string.

  • @alphanumeric: Requires a string to be alphanumeric.

  • @lowercase: Validates a string to be in lowercase.

  • @uppercase: Ensures a string is in uppercase.

  • @lowercase_alphanumeric: Validates a lowercase alphanumeric string.

  • @uppercase_alphanumeric: Validates an uppercase alphanumeric string.

  • @email: Checks if a string is a valid email.

  • @regex: Checks if string matches given regex

Integer/Float Annotations

  • @less_than: Validates an integer/float to be less than a specified value.

  • @less_than_or_equal: Validates an integer/float to be less than or equal to a specified value.

  • @greater_than: Validates an integer/float to be greater than a specified value.

  • @greater_than_or_equal: Validates an integer/float to be greater than or equal to a specified value.

  • @equal_to: Validates an integer/float to be equal to a specified value.

  • @not_equal_to: Validates an integer/float to not be equal to a specified value.

List Annotations

  • @list_min_length: Validates the minimum length of a list.

  • @list_max_length: Validates the maximum length of a list.

Annotations for Other Types

  • @dive: Used for nested record validation, allowing validation of each element within a composite structure like records or lists.

Contributing

Contributions to validate are warmly welcomed and appreciated. Whether it's opening issues for bug reports, suggesting new features, or submitting pull requests, all forms of contribution help in making validate better.

Dependencies (5)

  1. uri
  2. re
  3. ppx_deriving
  4. dune >= "3.12"
  5. ocaml >= "5.0.0"

Dev Dependencies (2)

  1. odoc with-doc
  2. alcotest with-test

Used by

None

Conflicts

None

OCaml

Innovation. Community. Security.