Skip to content

Conversation

john-h-kastner-aws
Copy link
Contributor

Description of changes

Add help messages on to type errors where we can suggest a useful fix. For example, an error from using in with a non-entity on the left will suggest using contains, containsAll or containsAny instead.

Issue #, if available

Checklist for requesting a review

The change in this PR is (choose one, and delete the other options):

  • A bug fix or other functionality change requiring a patch to cedar-policy.

I confirm that this PR (choose one, and delete the other options):

  • Updates the "Unreleased" section of the CHANGELOG with a description of my change (required for major/minor version bumps).

I confirm that cedar-spec (choose one, and delete the other options):

  • Does not require updates because my change does not impact the Cedar Dafny model or DRT infrastructure.

Disclaimer

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Comment on lines +483 to +503
pub(crate) fn suggested_has_guard(&self) -> String {
// We know if this is an access directly on `context`, so we can suggest
// specifically `context has ..`. Otherwise, we just use a generic `e`.
let base_expr = match self {
AttributeAccess::Context(_, _) => "context".into(),
_ => "e".into(),
};

let (safe_attrs, err_attr) = match self.attrs().split_first() {
Some((first, rest)) => (rest, first.clone()),
// We should always have a least one attribute stored, so this
// shouldn't be possible. If it does happen, just use a placeholder
// attribute name `f` since we'd rather avoid panicking.
None => (&[] as &[SmolStr], "f".into()),
};

let full_expr = std::iter::once(&base_expr)
.chain(safe_attrs.iter().rev())
.join(".");
format!("{full_expr} has {err_attr}")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This is complicated enough that it might make sense to have a test checking that the generated help message is sensible. maybe in particular for a case like context.foo.bar has spam.

@john-h-kastner-aws john-h-kastner-aws merged commit 46ffccc into main Dec 18, 2023
@john-h-kastner-aws john-h-kastner-aws deleted the feature/jkastner/validator_helps branch December 19, 2023 15:26
@cdisselkoen cdisselkoen added the 3.1 Features for 3.1 label Feb 6, 2024
shaobo-he-aws pushed a commit that referenced this pull request May 20, 2025
Signed-off-by: Craig Disselkoen <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.1 Features for 3.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants