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

Validate variable keys as part of schema #2530

Merged
merged 1 commit into from
May 27, 2024

Conversation

itowlson
Copy link
Contributor

@itowlson itowlson commented May 24, 2024

This works towards #2520 by enforcing variable key constraints in the manifest schema instead of at trigger time: this allows them to get picked up by spin build.

There's a risk this earlier validation could cause something to break, or to break in different ways. (Indeed I see some unit tests that I forgot to run already going a cheery red.) In particular, the error message at spin up changes from:

# Before
Error: invalid variable name: "DB_URL": invalid character 'D'. Variable names may contain only lower-case letters, numbers, and underscores.

to:

# After
Error: TOML parse error at line 10, column 1
   |
10 | DB_URL = { default = "..." }
   | ^^^^^^
Lower-case identifiers must be all lowercase; got Id("DB_URL")

which is slightly less specific in terms of explaining the error but slightly more specific in terms of locating it in the TOML!

There is also, I guess, a risk that this could block something that was legal before, but I'm pretty sure it doesn't. Not sure how to validate this other than by the power of reviewers' brains!

I completely understand if folks feel this is not worth the extra code or the extra risk, just putting it out there if we want it.

Quick note on the implementation: I layered this onto the Id type rather than the raw String type. This arguably creates unnecessary indirection, but means it is guaranteed to inherit tweaks or bug fixes to the Id reader if there ever are any. I'm happy to back this out and put it directly over String with a shared helper for the separator stuff if people prefer!

@itowlson itowlson requested a review from lann May 24, 2024 02:34
@itowlson
Copy link
Contributor Author

The unit tests that fail are angry about the capitalised TWO in this line (and its v1 and spin-doctor equivalents):

var_TWO = { required = true, secret = true }

My understanding is that this is an illegal name and would be rejected when the application ran. Are we happy to modify these UI tests, or is the mixed-case variable name intentional? I'm happy to close this PR if we want to retain this existing behaviour.

Copy link
Collaborator

@lann lann left a comment

Choose a reason for hiding this comment

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

A couple of non-load-bearing comments

@@ -8,6 +8,11 @@ use serde::{Deserialize, Serialize};
#[serde(into = "String", try_from = "String")]
pub struct Id<const DELIM: char>(String);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think I would either make this even more generic:

Suggested change
pub struct Id<const DELIM: char>(String);
pub struct Id<const DELIM: char, const LOWER: bool>(String);

or flatten these all out into separate impls.

🤷

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The even more generic approach works out really nicely. Thanks!

@@ -149,3 +149,11 @@ fn id_from_string<const DELIM: char>(id: String) -> Result<spin_serde::id::Id<DE
.try_into()
.map_err(|err: String| Error::InvalidID { id, reason: err })
}

fn lower_id_from_string<const DELIM: char>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Git tells me I wrote id_from_string; why didn't I make a TryFrom<String> I wonder? 🤔

@lann
Copy link
Collaborator

lann commented May 24, 2024

The unit tests that fail are angry about the capitalised TWO in this line

I think I was vaguely thinking about forward-compatibility with value imports (which will allow mixed case like this), but given that the env provider can't currently distinguish casing like this I think its fine to enforce lowercase for now.

Signed-off-by: itowlson <ivan.towlson@fermyon.com>
@itowlson itowlson merged commit bdb8c4b into fermyon:main May 27, 2024
17 checks passed
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