Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema
Validation::is_valid_schema
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() Validation::is valid schema code WC 10.7.0
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 ) ) {
// 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;
}