Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema
Validation::validate_document_object
Validate the field rules.
Method of the class: Validation{}
No Hooks.
Returns
true|false|WP_Error.
Usage
$result = Validation::validate_document_object( $document_object, $rules );
- $document_object(DocumentObject) (required)
- The document object to validate.
- $rules(array) (required)
- The rules to validate against.
Validation::validate_document_object() Validation::validate document object code WC 10.7.0
public static function validate_document_object( DocumentObject $document_object, $rules ) {
if ( self::schema_is_unwrapped( $rules ) ) {
$rules = [
'$schema' => 'http://json-schema.org/draft-07/schema#',
'type' => 'object',
'properties' => $rules,
];
} else {
if ( ! isset( $rules['$schema'] ) ) {
$rules['$schema'] = 'http://json-schema.org/draft-07/schema#';
}
if ( ! isset( $rules['type'] ) ) {
$rules['type'] = 'object';
}
}
try {
$validator = new Validator();
$result = $validator->validate(
Helper::toJSON( $document_object->get_data() ),
Helper::toJSON( $rules )
);
if ( ! $result->hasError() ) {
return true;
}
} catch ( \Exception $e ) {
return new WP_Error( 'woocommerce_rest_checkout_validation_failed', __( 'Validation failed.', 'woocommerce' ) );
}
// Return generic error message.
return new WP_Error( 'woocommerce_rest_checkout_invalid_field', __( 'Invalid field.', 'woocommerce' ) );
}