Início / Optitravel / Helpers / ScimHelper
fieldsValidation(array $rules, $object)
This method validates the fields of a given object using a set of rules, leveraging the ValidatorHelper class.
If any of the validations fail, it throws an HTTP 400 error with a detailed structure of validation errors, using ApiHelper::error().
Parameters
-
$rules – (array) Required. An array of validation rules where the key is the field name and the value is a pipe-separated string of rules.
-
$object – (object) Required. An object with a toArray() method that returns an associative array of fields to be validated.
Behavior
This function:
-
Converts the object into an array using $object->toArray().
-
Calls ValidatorHelper::validate(...) with the array, rules, and the 'SCIM' protocol identifier.
-
Inside ValidatorHelper, a wide set of validations can be performed, including:
-
mandatory / optional
-
int, string, email
-
min:n, max:n
-
exists[:table,column] and not_exists[:table,column] rules for DB validations
-
If any rule fails, a descriptive error message is returned immediately with HTTP code 400.
Example
use \App\Utils\ScimHelper;
$rules = [
'username' => 'mandatory|string|min:3|max:20|not_exists:users,username',
'email' => 'mandatory|email|not_exists@optitravel:users,email&system_id=\'W2M\'',
];
$userObject = new MyUserModel();
ScimHelper::fieldsValidation($rules, $userObject);
if the validation passes, the code continues executing silently. If validation fails, the system halts and returns a structured error response like:
{
"schemas": [
"urn:ietf:params:scim:api:messages:2.0:Error"
],
"scimType": "uniqueness",
"detail": "O valor email já existe.",
"status": "409"
}
↑ (Última atualização: 14/05/2025)