Skip to main content

Tiny runtime validators for explicit precondition checks

Project description

requireit

Tiny, numpy-aware runtime validators for explicit precondition checks.

requireit provides a small collection of lightweight helper functions such as require_positive, require_between, and require_array for validating values and arrays at runtime.

It is intentionally minimal and dependency-light (numpy only).

Why requireit?

  • Explicit – reads clearly
  • numpy-aware – works correctly with scalars and arrays
  • Fail-fast – raises immediately with clear error messages
  • Lightweight – just a bunch of small functions
  • Reusable – avoids copy-pasted validation code across projects
from requireit import require_one_of
from requireit import require_positive

require_positive(dt)
require_one_of(method, allowed={"foo", "bar"})

Design principles

  • Prefer small, single-purpose functions
  • Raise standard exceptions (ValidationError)
  • Never coerce or "fix" invalid inputs
  • Validate all elements for array-like inputs
  • Keep the public API small

Non-goals

requireit is not:

  • a schema or data-modeling system
  • a replacement for static typing
  • a validation framework
  • a substitute for unit tests
  • a coercion or parsing library

If you need structured validation, transformations, or user-facing error aggregation, you probably want something heavier.

Installation

pip install requireit

API Summary

All validators:

  • validate the first argument
  • return the original value/array on success
  • raise ValidationError on failure

Arrays

  • require_array: Validate an array to satisfy requirements.
  • require_shape: Validate that an array has the specified shape.

General

  • require_contains: Require collection contains required values.
  • require_not_one_of: Require value is not contained in forbidden
  • require_one_of: Require value is contained in allowed

Length

  • require_length: Require len(value) == length
  • require_length_at_least: Require len(value) >= length
  • require_length_at_most: Require len(value) <= length
  • require_length_between: Require len(value) falls within a specified range.

Numeric

  • require_between: Validate that a value lies within a specified interval.
  • require_greater_than: Require value > lower
  • require_greater_than_or_equal: Require value >= lower
  • require_less_than: Require value < upper
  • require_less_than_or_equal: Require value <= upper
  • require_negative: Require value < 0
  • require_nonnegative: Require value >= 0
  • require_nonpositive: Require value <= 0
  • require_positive: Require value > 0

Paths

  • require_path_string: Validate that a value is a string intended to be used as a path.

Errors

All validation failures raise:

requireit.ValidationError

This allows callers to catch validation failures distinctly from other errors.

Contributing

This project is intentionally small.

Contributions should preserve:

  • minimal surface area
  • explicit semantics
  • no additional dependencies

If a proposed change needs much explanation, it probably doesn’t belong here.

Credits

Development Leads

Release Notes

0.5.0 (2026-03-28)

Features

  • Extended require_array to allow flexible shape validation with support for wildcard dimensions (None or named axes).

0.4.0 (2026-03-27)

Features

  • Added require_greater_than, require_greater_than_or_equal, and require_less_than_or_equal validators. #26

0.3.0 (2026-03-23)

Features

  • Added require_not_one_of validator to ensure a value is not in a forbidden set #15
  • Added length validators to check that an object’s length is exactly, at most, or at least a given value #16
  • Added require_length_between validator to check that an object’s length is within a specified range #19
  • Added require_contains validator to ensure a collection contains required values #21
  • Added import_package validator to check for and import a package #22
  • Added argparse_type to allow requireit validators to be used as argparse type= callables #17

Changes

  • Renamed length validators for consistency: require_length_isrequire_length, require_length_is_at_leastrequire_length_at_least, require_length_is_at_mostrequire_length_at_most #20

Tests

  • Added unit tests to verify that validators return the input value (not a copy) on success #18

0.2.0 (2026-01-16)

  • Standardized validation error messages #8
  • Renamed validate_array to require_array #9
  • Added optional name keyword to require functions to make error messages easier to read #10
  • Added new validator, require_path_string, that checks if a value could be used as a file path #11
  • Added new validator, require_less_than, that checks if one value is less than another #12

0.1.0 (2026-01-12)

  • Added documentation to the README #1
  • Added project metadata files #2
  • Added the requireit module #3
  • Added pyproject.toml file #4
  • Added noxfile.py file and linters #5
  • Added unit tests for requireit #6
  • Added GitHub Actions for CI #7

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

requireit-0.5.0.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

requireit-0.5.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file requireit-0.5.0.tar.gz.

File metadata

  • Download URL: requireit-0.5.0.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for requireit-0.5.0.tar.gz
Algorithm Hash digest
SHA256 2ce63c3eb4dc7e3ab8ad268d28a54da8eae8022fa5866894ba9817ffd2b738bd
MD5 301a60a9d140d9cac96dd98ccbd2833a
BLAKE2b-256 1a2bb9d24c44db88d93a078a0a5f1b5cce8f9cd830c2a230559da87d724ce11b

See more details on using hashes here.

Provenance

The following attestation bundles were made for requireit-0.5.0.tar.gz:

Publisher: ci.yml on mcflugen/requireit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file requireit-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: requireit-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for requireit-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3b33b729843e39f245ddbf9f0c00195ddd6038c5a721da9237a5cb7a228e5e7c
MD5 be0a893828fa968a47f33f28471d6d99
BLAKE2b-256 e3434f1b67d850dd1ef4f66f073e9ea2aa889a4fadd7f5c305d2102a6f06fa84

See more details on using hashes here.

Provenance

The following attestation bundles were made for requireit-0.5.0-py3-none-any.whl:

Publisher: ci.yml on mcflugen/requireit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page