Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

Validation::is_valid_schema │ public static │ WC 1.0

Validate meta schema for field rules.

Method of the class: Validation{}

No Hooks.

Returns

bool|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 11.2.1

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 ( self::schema_is_unwrapped( $rules ) ) {
		$rules = [
			'type'       => 'object',
			'properties' => $rules,
		];
	}

	if ( empty( self::$meta_schema_json ) ) {
		self::$meta_schema_json = self::build_meta_schema();
	}

	$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
	);

	$error = $result->error();
	if ( null !== $error ) {
		return new WP_Error( 'woocommerce_rest_checkout_invalid_field_schema', self::format_schema_error( $error ) );
	}

	return true;
}