rest_validate_string_value_from_schema

The timeline below displays how wordpress function rest_validate_string_value_from_schema has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 7.1

/**
 * Validates a string value based on a schema.
 *
 * @since 5.7.0
 *
 * @param mixed  $value The value to validate.
 * @param array  $args  Schema array to use for validation.
 * @param string $param The parameter name, used in error messages.
 * @return true|WP_Error
 */
function rest_validate_string_value_from_schema($value, $args, $param)
{
    if (!is_string($value)) {
        return new WP_Error(
            'rest_invalid_type',
            /* translators: 1: Parameter, 2: Type name. */
            sprintf(__('%1$s is not of type %2$s.'), $param, 'string'),
            array('param' => $param)
        );
    }
    if (isset($args['minLength']) && mb_strlen($value) < $args['minLength']) {
        return new WP_Error('rest_too_short', sprintf(
            /* translators: 1: Parameter, 2: Number of characters. */
            _n('%1$s must be at least %2$s character long.', '%1$s must be at least %2$s characters long.', $args['minLength']),
            $param,
            number_format_i18n($args['minLength'])
        ));
    }
    if (isset($args['maxLength']) && mb_strlen($value) > $args['maxLength']) {
        return new WP_Error('rest_too_long', sprintf(
            /* translators: 1: Parameter, 2: Number of characters. */
            _n('%1$s must be at most %2$s character long.', '%1$s must be at most %2$s characters long.', $args['maxLength']),
            $param,
            number_format_i18n($args['maxLength'])
        ));
    }
    if (isset($args['pattern']) && !rest_validate_json_schema_pattern($args['pattern'], $value)) {
        return new WP_Error(
            'rest_invalid_pattern',
            /* translators: 1: Parameter, 2: Pattern. */
            sprintf(__('%1$s does not match pattern %2$s.'), $param, $args['pattern'])
        );
    }
    return true;
}

WordPress Version: 5.7

/**
 * Validates a string value based on a schema.
 *
 * @since 5.7.0
 *
 * @param mixed  $value The value to validate.
 * @param array  $args  Schema array to use for validation.
 * @param string $param The parameter name, used in error messages.
 * @return true|WP_Error
 */
function rest_validate_string_value_from_schema($value, $args, $param)
{
    if (!is_string($value)) {
        return new WP_Error(
            'rest_invalid_type',
            /* translators: 1: Parameter, 2: Type name. */
            sprintf(__('%1$s is not of type %2$s.'), $param, 'string'),
            array('param' => $param)
        );
    }
    if (isset($args['minLength']) && mb_strlen($value) < $args['minLength']) {
        return new WP_Error(
            'rest_too_short',
            /* translators: 1: Parameter, 2: Number of characters. */
            sprintf(_n('%1$s must be at least %2$s character long.', '%1$s must be at least %2$s characters long.', $args['minLength']), $param, number_format_i18n($args['minLength']))
        );
    }
    if (isset($args['maxLength']) && mb_strlen($value) > $args['maxLength']) {
        return new WP_Error(
            'rest_too_long',
            /* translators: 1: Parameter, 2: Number of characters. */
            sprintf(_n('%1$s must be at most %2$s character long.', '%1$s must be at most %2$s characters long.', $args['maxLength']), $param, number_format_i18n($args['maxLength']))
        );
    }
    if (isset($args['pattern']) && !rest_validate_json_schema_pattern($args['pattern'], $value)) {
        return new WP_Error(
            'rest_invalid_pattern',
            /* translators: 1: Parameter, 2: Pattern. */
            sprintf(__('%1$s does not match pattern %2$s.'), $param, $args['pattern'])
        );
    }
    return true;
}