Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

Validation::validate_document_objectpublic staticWC 1.0

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() code WC 9.8.5

public static function validate_document_object( DocumentObject $document_object, $rules ) {
	try {
		$validator = new Validator();
		$result    = $validator->validate(
			Helper::toJSON( $document_object->get_data() ),
			Helper::toJSON(
				[
					'$schema'    => 'http://json-schema.org/draft-07/schema#',
					'type'       => 'object',
					'properties' => $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' ) );
}