Validates schemas and datatypes in a simple way, strongly inspired by Zod
Creating a simple integer schema
include_once "./zschema.php";
// creating the integer schema
$int_schema = ZSchema::int();
// parsing
print_r($int_schema->safe_parse(5)); // output: ["success" => true]
print_r($int_schema->safe_parse("hello")); // output: ["success" => false, "message" => ...]
Creating a array schema
include_once "./zschema.php";
// creating user schema
$user_schema = ZSchema::array([
"first_name" => ZSchema::string()->min_length(3)->required(),
"last_name" => ZSchema::string()->min_length(3),
"email" => ZSchema::string()->email()->required(),
]);
print_r($user_schema->safe_parse([
"first_name" => "John",
"last_name" => "Doe",
"email" => "john@doe.com",
])); // output: ["success" => true]
print_r($user_schema->safe_parse([
"first_name" => "John",
"last_name" => "Doe",
"email" => "johndoe.com",
])); // output: ["success" => false, "message" => "email is not a valid email"]
ZSchema::int()
ZSchema::float()
ZSchema::string()
ZSchema::bool()
ZSchema::array()
ZSchema::null()
Strings have many types of specific validations
ZSchema::string()->required()
ZSchema::string()->not_empty()
ZSchema::string()->max_length() // the arg must be a integer, example: max_length(5)
ZSchema::string()->min_length() // the arg must be a integer, example: min_length(5)
ZSchema::string()->length() // the arg must be a integer, example: max_length(5)
ZSchema::string()->email()
ZSchema::string()->url()
ZSchema::string()->uuid()
ZSchema::string()->ipv4()
ZSchema::string()->ipv6()
ZSchema::string()->regex() // the arg must be a regex
ZSchema::string()->includes() // the arg must be a string, example: includes("http")
ZSchema::string()->not_includes() // the arg must be a string, example: not_includes("google")
ZSchema::string()->starts_with() // the arg must be a string, example: starts_with("http")
ZSchema::string()->not_starts_with() // the arg must be a string, example: not_starts_with("http")
ZSchema::string()->ends_with() // the arg must be a string, example: ends_with(".com")
ZSchema::string()->not_ends_with() // the arg must be a string, example: not_ends_with(".exe")
ZSchema::string()->date() // under review
ZSchema::string()->time() // under review
ZSchema::string()->datetime() // under review
The transforms methods modify the value returned by the parse
ZSchema::string()->trim()
ZSchema::string()->to_lower_case()
ZSchema::string()->to_upper_case()
Example
echo ZSchema::string->to_lower_case()->parse("Hello World!") // output: "hello world!"
Validation and transformations methods work for both int and float
ZSchema::int()
ZSchema::float()
ZSChema::int()->required()
ZSChema::int()->not_empty()
ZSChema::int()->max() // the arg must be a integer, example: max(100)
ZSChema::int()->min() // the arg must be a integer, example: min(0)
ZSChema::int()->positive()
ZSChema::int()->nonpositive()
ZSChema::int()->negative()
ZSChema::int()->nonnegative()
The transforms methods modify the value returned by the parse
ZSchema::int()->to_max() // the arg must be a integer, example: to_max(100)
ZSchema::int()->to_min() // the arg must be a integer, example: to_min(0)
Example
echo ZSchema::int->to_max(25)->parse(10000) // output: 25
The value of the array keys must be an instance of ZSchema, otherwise it will throw an error when creating a schema.
Example:
// BAD
ZSchema::array([
"email" =>...
])
// GOOD
ZSchema::array([
"email" => ZSchema::string()->email()
])
the value of the key can be any type of zschema
ZSchema::array([
"day" => ZSchema::int()
])
The parse method executes the validations specified in the method value, if the validation fails it will throw an exception with an error message
ZSchema::int()->parse(5) // return 5
ZSchema::int()->parse("hola") // throws Error
Unlike the parse method, when the validation fails it will not throw an error, instead it will return an array with the message and the status of the validation.
ZSchema::int()->safe_parse(5) // return ["success" => true, "value" => 5]
ZSchema::int()->safe_parse("hola") // return ["success" => false, message => ..., "value" => "hola"]
ZSchema::string()->email()->get_validations() // return ["email" => true]
ZSchema::string()->to_lower_case()->get_transforms() // return ["to_lower_case" => true]
// by default
ZSchema::int()->safe_parse("world") // return ["sucess" => false, "message" => "world is not a valid int", ...]
// with custom type error message
ZSchema::int("The value is not a number")->safe_parse("world") // return ["sucess" => false, "message" => "The value is not a number", ...]
but for arrays the second argument is the message
$user_schema = ZSchema::array([
"first_name" => ZSchema::string()->min_length(3)->required(),
"last_name" => ZSchema::string()->min_length(3),
"email" => ZSchema::string()->email()->required(),
], "The user value is not valid");
for validations it is a bit more of the same, in validations where no argument is required to validate, the argument will be the error message, if the validation method has an argument, then it will be the second argument
ZSchema::string()->email("The e-mail is not valid")->max_length(100, "the e-mail must not contain more than 100 characters")