Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFieldsSchema

Validation::format_schema_error │ private static │ WC 1.0

Describes schema errors at the failing keyword instead of the outer schema wrapper.

Method of the class: Validation{}

No Hooks.

Returns

string. The error paths and messages.

Usage

$result = Validation::format_schema_error( $error ): string;
$error(ValidationError) (required)
The schema validation error.

Validation::format_schema_error() code WC 11.2.1

private static function format_schema_error( ValidationError $error ): string {
	$formatter = new ErrorFormatter();
	$errors    = $formatter->format(
		$error,
		true,
		static function ( ValidationError $error ) use ( $formatter ): string {
			if ( 'const' === $error->keyword() ) {
				return sprintf( 'The value must be %s', wp_json_encode( $error->args()['const'] ) );
			}
			$schema = $error->schema()->info()->data();
			if ( 'enum' === $error->keyword() && is_object( $schema ) && isset( $schema->enum ) ) {
				return sprintf( 'The value must be one of: %s', implode( ', ', array_map( 'wp_json_encode', $schema->enum ) ) );
			}
			return $formatter->formatErrorMessage( $error );
		}
	);
	$messages  = [];
	foreach ( $errors as $path => $details ) {
		// The properties/test prefix belongs to our meta-schema check, not the supplied rule.
		$path       = rawurldecode( preg_replace( '#^/properties/test(?=/|$)#', '', $path ) ?? $path );
		$messages[] = sprintf( 'At "%s": %s.', $path ? $path : '/', implode( '; ', $details ) );
	}

	return implode( ' ', $messages );
}