Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

Validation::is_valid_schema()public staticWC 1.0

Validate meta schema for field rules.

Метод класса: Validation{}

Хуков нет.

Возвращает

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

Использование

$result = Validation::is_valid_schema( $rules );
$rules(массив) (обязательный)
The field rules.

Код Validation::is_valid_schema() WC 9.8.5

public static function is_valid_schema( $rules ) {
	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();
	$test_schemas = [ 'required', 'hidden', 'validation' ];

	foreach ( $test_schemas as $rule ) {
		if ( empty( $rules[ $rule ] ) ) {
			continue;
		}
		if ( ! is_array( $rules[ $rule ] ) ) {
			return new WP_Error( 'woocommerce_rest_checkout_invalid_field_schema', sprintf( 'The %s rules must be an array.', esc_html( $rule ) ) );
		}
		$result = $validator->validate(
			Helper::toJSON(
				[
					'$schema'    => 'http://json-schema.org/draft-07/schema#',
					'type'       => 'object',
					'properties' => [
						'test' => $rules[ $rule ],
					],
					'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;
}