Skip to content

Using Valicomb for Input Validation

Valicomb is a simple, minimal PHP validation library with zero dependencies. Built for PHP 8.2+ with a security-first design, it’s well suited for validating input data in your REST API built with Slim framework.

Key benefits:

  • 54 built-in validation rules across strings, numbers, dates, arrays, and networks
  • Multiple syntaxes for defining rules (field-based arrays, fluent API, chaining)
  • Built-in internationalization support (33 languages)
  • Security protections against ReDoS attacks, type juggling, and injection
  • No external dependencies, making it lightweight and easy to integrate into any PHP project
  • Clear error messages

Install Valicomb using Composer:

Terminal window
composer require frostybee/valicomb
  • However, Valicomb is already included in the provided Slim template. So you can skip this step.

  1. Import Valicomb in your route handler

    use Frostybee\Valicomb\Validator;
  2. Get request data and create validator

    $app->post('/api/planets', function ($request, $response) {
    // Get the associative array of data from request.
    $data = $request->getParsedBody();
    // Instantiate a validator and initialize it with the data to be validated.
    $v = new Validator($data);
    });
  3. Define validation rules as an array organized by field

    // Array with field names as keys.
    $rules = [
    'name' => ['required', ['lengthMin', 2]],
    'type' => ['required', ['in', ['Terrestrial', 'Gas Giant', 'Ice Giant', 'Dwarf']]],
    'radius' => ['required', 'numeric', ['min', 0]],
    'moons' => ['required', 'integer', ['min', 0]]
    ];
    // Apply the rules using forFields.
    $v->forFields($rules);
  4. Validate and handle errors

    if ($v->validate()) {
    // Validation passed - process data.
    $response->getBody()->write(json_encode([
    'success' => true,
    'message' => 'Planet created successfully'
    ]));
    return $response->withStatus(201);
    } else {
    // Validation failed - return errors.
    $response->getBody()->write(json_encode([
    'success' => false,
    'errors' => $v->errors()
    ]));
    return $response->withStatus(422);
    }

Valicomb provides two ways to specify validation rules as arrays:

  1. Field-based array (field names as keys)
    $rules = [
    'name' => ['required', ['lengthMin', 2]]
    ];
    $v->forFields($rules);
  2. Rule-based array (rule names as keys)
    $rules = [
    'required' => ['name'],
    'lengthMin' => [['name', 2]]
    ];
    $v->rules($rules);

The following table contains some of the most common validation rules that you can use with Valicomb. For a complete list of validation rules, visit the Valicomb documentation.

RuleExampleDescription
required$v->rule('required', 'name')Field must be present and not empty
numeric$v->rule('numeric', 'radius')Must be a number
integer$v->rule('integer', 'moons')Must be an integer
lengthMin$v->rule('lengthMin', 'name', 2)Minimum string length
lengthMax$v->rule('lengthMax', 'name', 50)Maximum string length
min$v->rule('min', 'radius', 0)Minimum numeric value
max$v->rule('max', 'radius', 100000)Maximum numeric value
email$v->rule('email', 'contact')Valid email format
url$v->rule('url', 'website')Valid URL format
in$v->rule('in', 'type', ['Terrestrial', 'Gas Giant'])Value must be in list
alpha$v->rule('alpha', 'name')Alphabetic characters only
boolean$v->rule('boolean', 'active')Boolean-like values
date$v->rule('date', 'launch_date')Valid date value

POST /api/planets endpoint with field-based array validation
use Frostybee\Valicomb\Validator;
$app->post('/api/planets', function ($request, $response) {
// Associative array of data to validate.
$data = $request->getParsedBody();
// Example: ['name' => 'Mars', 'type' => 'Terrestrial', 'radius' => 3389, 'moons' => 2]
// Instantiate a validator and initialize it with the data to be validated.
$v = new Validator($data);
// Array of validation rules organized by field name.
$rules = [
'name' => ['required', ['lengthMin', 2]],
'type' => ['required', ['in', ['Terrestrial', 'Gas Giant', 'Ice Giant', 'Dwarf']]],
'radius' => ['required', 'numeric', ['min', 0]],
'moons' => ['required', 'integer', ['min', 0]]
];
// Apply the rules.
$v->forFields($rules);
// Validate.
if ($v->validate()) {
// Success - process the data.
$response->getBody()->write(json_encode([
'success' => true,
'message' => 'Planet data is valid',
'data' => $data
]));
return $response->withHeader('Content-Type', 'application/json')->withStatus(200);
} else {
// Failure - return errors.
$response->getBody()->write(json_encode([
'success' => false,
'message' => 'Validation failed',
'errors' => $v->errors()
]));
return $response->withHeader('Content-Type', 'application/json')->withStatus(422);
}
});

Valicomb also provides a fluent API that chains validation methods per field with full IDE autocomplete support:

Example using the fluent field builder
use Frostybee\Valicomb\Validator;
$app->post('/api/planets', function ($request, $response) {
$data = $request->getParsedBody();
$v = new Validator($data);
// Define rules using the fluent field builder.
$v->field('name')
->required()
->lengthMin(2);
$v->field('type')
->required()
->in(['Terrestrial', 'Gas Giant', 'Ice Giant', 'Dwarf']);
$v->field('radius')
->required()
->numeric()
->min(0);
$v->field('moons')
->required()
->integer()
->min(0);
if ($v->validate()) {
$response->getBody()->write(json_encode([
'success' => true,
'message' => 'Planet data is valid'
]));
return $response->withStatus(200);
} else {
$response->getBody()->write(json_encode([
'success' => false,
'errors' => $v->errors()
]));
return $response->withStatus(422);
}
});