Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

Validation::is_valid_schemapublic staticWC 1.0

Validate meta schema for field rules.

Method of the class: Validation{}

No Hooks.

Returns

true|false|WP_Error. True if the field options are valid, a WP_Error otherwise.

Usage

$result = Validation::is_valid_schema( $rules );
$rules(mixed) (required)
The rules to validate.

Validation::is_valid_schema() code WC 9.9.4

public static function is_valid_schema( $rules ) {
	if ( ! is_array( $rules ) ) {
		return new WP_Error( 'woocommerce_rest_checkout_invalid_field_schema', 'Rules must be defined as an array.' );
	}

	if ( empty( $rules ) ) {
		return true;
	}

	if ( empty( self::$meta_schema_json ) ) {
		// phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		self::$meta_schema_json = file_get_contents( __DIR__ . '/json-schema-draft-07.json' );
	}

	$validator = new Validator();
	$result    = $validator->validate(
		Helper::toJSON(
			[
				'$schema'    => 'http://json-schema.org/draft-07/schema#',
				'type'       => 'object',
				'properties' => [
					'test' => $rules,
				],
				'required'   => [ 'test' ],
			]
		),
		self::$meta_schema_json
	);

	if ( $result->hasError() ) {
		return new WP_Error( 'woocommerce_rest_checkout_invalid_field_schema', esc_html( (string) $result->error() ) );
	}

	return true;
}