Skip to content

Ability to provide type hints to non-identifier keys #871

Open

Description

@stopachka

Consider a JSON object

{ 
   "with-hyphen": "a-string", 
   "$startsWithDollarSign": 123
}

Problem: It's not possible to provide type-hints for with-hyphen and $startsWithDolalrSign

CEL can get type information well if we can express an object as protobuf, or use StructType. However, there are times when we have objects that have non-identifier keys.

Right now, it's not possible to provide typesafety for non-identifier keys.

It would be amazing if we could.

Alternatives considered

To solve this for ourselves, we considered to hacks:

  1. Use aliases: walk the CEL tree, and rename all non-identifer keys to some known identifer-style key.
  2. Run our own compile step validation: walk the cel tree, and do the type checking ourselves.

Right now we are going with 2., but if there was some built-in way to specify types for keys from index access, it would be amazing.

Activity

  1. changed the title [-]Ability[/-] [+]Ability to provide type hints to non-identifier keys[/+] on Nov 21, 2025
  2. l46kok commented on Nov 21, 2025

    @l46kok
    Collaborator

    As an alternative: you could toggle the following option to allow backticks as an escape mechanism to allow field accesses outside normal identifiers for structs/protobufs: https://github.com/google/cel-java/blob/main/common/src/main/java/dev/cel/common/CelOptions.java#L272

    foo.`with-hyphen`
    

    Though at the moment, "$" isn't supported, but this is something we could add:
    https://github.com/google/cel-java/blob/d7ad94d02c982fd465fcf472802a5be87766ff18/parser/src/main/java/dev/cel/parser/gen/CEL.g4#L207

    More generally, we considered adding first-party support for JsonSchema and Open API schema. That would address this use case. I'll keep this open until we add that in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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