Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: treat empty string as a nil value in deser #129

Closed
wants to merge 1 commit into from

Conversation

maxveldink
Copy link
Owner

There's a significant pain point when deserializing optional values over JSON (and in hashes) when null is not used, and instead, an empty string is present for the absence of a value. This slightly modifies deserialization to treat empty strings as nil, as anecdotally this has been expected behavior.

Copy link
Collaborator

@antoinesaliba antoinesaliba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚢

@@ -38,6 +38,8 @@ def deserialize_from_creation_params(creation_params)

if value.nil? && !field.default.nil?
Success.new(Validations::ValidatedValue.new(name: field.name, value: field.default))
elsif !field.required && (value.nil? || value == "")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nitpick) the if before and the elsif after it have the value check first and then the field check so it's a bit faster to read as if we follow that ordering here elsif (value.nil? || value == "") && !field.required

@maxveldink
Copy link
Owner Author

Closing this, as it's an opinionated change that probably needs to be handled outside of sorbet-schema

@maxveldink maxveldink closed this Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants