Skip to content

Use custom annotations to override type validation during JSON to Typed conversion #1331

@avantikasparihar

Description

@avantikasparihar

Problem Statement
We have a use case where we need to selectively bypass type validation for properties marked with custom annotations during JSON-to-Typed object conversion.

Current Behavior
When using deepToTyped converter to transform JSON data into Pkl Typed objects, all properties undergo strict type validation. For example, if a property is defined as String, the converter will fail if the JSON contains a non-string value.

Desired Behavior
We want to define custom annotations (e.g., @secret) that, when applied to a property, allow the converter to bypass type validation for that specific property.

Use Case Example
Pkl Schema Definition:

class DatabaseConfig {
  host: String
  port: Int
  
  @Secret
  password: String  // Should accept any JSON value type, not just strings
}

Input JSON:

{
  "host": "localhost",
  "port": 5432,
  "password": {
    "$secret": {
      "type": "",
      "name": "db_password"
    }
  }
}

Please suggest the best approach to achieve this.

  1. What would be the most idiomatic way to handle annotation-based type validation bypassing in Pkl?
  2. Are there existing patterns or mechanisms in Pkl that we should leverage for this use case?

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