Skip to content

LinkAlternate name validation rejects empty string #104

Description

@Ninerian

Summary

The LinkAlternate constructor rejects link alternates when the name field is an empty string (""), even though empty strings are semantically equivalent to null (no name set).

Root Cause

In src/Domain/Value/LinkAlternate.php:41, the code checks:

if (null !== $values['name']) {
    $name = TrimmedNonEmptyString::fromString(\$values['name'])->toString();
}

An empty string ("") passes the null check but then fails the TrimmedNonEmptyString validation, throwing:

Webmozart\\Assert\\InvalidArgumentException: Expected a different value than "".

Observed Impact

We've encountered this in development environment when certain link alternates in Storyblok have empty strings instead of null for the name field. This causes the entire Links API response to fail during deserialization, blocking page rendering until the data is corrected or the library is patched.

Suggested Fix

Use !empty($values['name']) instead of null !== $values['name'] to treat empty strings and null identically:

if (!empty($values['name'])) {
    $name = TrimmedNonEmptyString::fromString($values['name'])->toString();
}

This approach is consistent with how other optional fields like anchor are handled (line 111), which already guards against both null and empty strings.

Why This Matters

From a data resilience perspective, it makes sense to treat empty strings and null the same way for optional fields. Storyblok may legitimately return either representation, and the client library should gracefully handle both without throwing exceptions.

Thanks for maintaining this library! 🙏

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions