paymobi/yay

PHP object schema validation for json post request handling

1.0.16 2022-08-15 17:11 UTC

This package is auto-updated.

Last update: 2025-06-15 23:36:51 UTC


README

How to install

  1. composer require paymobi/yay

How to use

<?php

use Yay\Yay;

$input = json_decode(file_get_contents("php://input"));

$schema = [
  "name" => Yay::item()->required()->string()->minLength(10)->maxLength(20),
  "age" => Yay::item()->required()->integer(),
  "cpf" => Yay::item()->required()->strHasOnlyDigits()->length(11),
  "skillLevel" => Yay::item()->required()->isNumberBetween([1, 5]),
  "weight" => Yay::item()->required()->float(),
  "birthdate" => Yay::item()->required()->isBrazilDateFormat(),
  "parents" => Yay::item()->required()->array()->length(2)->itemsOfType(
    Yay::item()->string()->minLength(10)->maxLength(20)
  ),
  "nickname" => Yay::item()->optional()->string(),
];

$errors = Yay::validate($schema, $input);


if ($errors) {
  echo("request body incorrect");
}

echo("request body correct");
</code>