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

update school validations to return list of errors for frontend use #270

Merged
merged 2 commits into from
Apr 24, 2024

Conversation

conorriches
Copy link
Member

@conorriches conorriches commented Apr 22, 2024

Issue

Related to https://github.com/RaspberryPiFoundation/editor-standalone/issues/28

What changed

When a school is trying to be created, but validations fail, we now get this response:

{
  "errors": {
    "website":["can't be blank"],
    "address_line_1":["can't be blank"],
    "municipality":["can't be blank"],
    "country_code":["can't be blank","is not included in the list"]
  }
}

This is useful, as the FE application can now iterate on the keys, and selectively update each input field to show the error message.

Before this change we got:

{"error":"Error creating school: Website can't be blank,Address line 1 can't be blank,Municipality can't be blank,Country code can't be blank,Country code is not included in the list"}

which isn't useful, as we can't associate errors to fields

@raspberrypiherokubot raspberrypiherokubot temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:09 Inactive
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:13 Inactive
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:37 Inactive
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:38 Inactive
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:43 Inactive
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:45 Inactive
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:48 Inactive
Copy link

cla-bot bot commented Apr 22, 2024

We require contributors to sign our Contributor License Agreement, and we don't have you on file. In order for us to review and merge your code, please complete this form and we'll get you added and review your contribution as soon as possible.

@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:51 Inactive
@RaspberryPiFoundation RaspberryPiFoundation deleted a comment from cla-bot bot Apr 22, 2024
@sra405
Copy link
Contributor

sra405 commented Apr 22, 2024

@cla-bot check

@cla-bot cla-bot bot added the cla-signed label Apr 22, 2024
Copy link

cla-bot bot commented Apr 22, 2024

The cla-bot has been summoned, and re-checked this pull request!

@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 22, 2024 15:56 Inactive
@conorriches conorriches self-assigned this Apr 23, 2024
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 23, 2024 11:33 Inactive
danhalson
danhalson previously approved these changes Apr 23, 2024
Copy link
Contributor

@danhalson danhalson left a comment

Choose a reason for hiding this comment

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

Looks great :)

@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 23, 2024 16:25 Inactive
report list of errored fields in lib

fix spec

fix typo

check for type

rubocop
@conorriches conorriches temporarily deployed to editor-api-p-school-val-thtc1o April 23, 2024 16:30 Inactive
Copy link
Contributor

@danhalson danhalson left a comment

Choose a reason for hiding this comment

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

perfect!

@chrislo
Copy link
Contributor

chrislo commented Apr 24, 2024

@conorriches have we thought about the i18n story around these error messages? I notice we do try to translate/localise strings that appear in editor-standalone, but I'm not sure how that would work here. I can think of two approaches off the top of my head 1) pass a language parameter in API requests so the API can return the correct version of the strings, or 2) return an identifier for each error instead of a string and use that to look up the localisation on the client side.

I'm also slightly nervous that we're relying on the keys (e.g. website) staying in sync between client and server. Have you thought about how to make sure they don't drift apart?

@chrislo
Copy link
Contributor

chrislo commented Apr 24, 2024

@conorriches - I should say I don't think these queries are caused by things you've introduced in this PR, I think we had the same two issues with the previous approach. I guess I'm mostly interested in having a cross-team approach to them to make sure we keep things consistent.

@conorriches
Copy link
Member Author

Hey @chrislo - yeah indeed a good thought, my running assumption was to keep the i18n on the frontend side of things, as we already have i18n set up there, and I was mostly relying on the backend just providing a list of fields that need flagging.

But it's a good point and I think we should look at i18n more broadly, specifically about how we want the api to respond to locales. The error messages appear to be coming out of rails itself, so passing in a locale and having i18n translations would probably be the best approach if we do want the api to be aware of i18n.

I've asked in slack about whether we will need locales - but so far in the frontend we only have the en locale, though if we plan to have full i18n support it would be good to define the boundaries for which side the translations will live on.

I'd like to understand more the concerns about keys staying in sync - the reason I felt safe doing this is that if they keys do go out of sync, then the endpoint won't be able to process the form properly as fields will be missing, meaning the form won't submit even without validation messages.

@conorriches
Copy link
Member Author

Discussed on Slack and we have captured the requirement in #272

@conorriches conorriches merged commit 0f63ea7 into main Apr 24, 2024
3 checks passed
@conorriches conorriches deleted the school-validations branch April 24, 2024 14:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants