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

No standard way to define schema for key in "Record-like" objects #1448

Closed
npdev453 opened this issue Sep 24, 2023 · 2 comments
Closed

No standard way to define schema for key in "Record-like" objects #1448

npdev453 opened this issue Sep 24, 2023 · 2 comments

Comments

@npdev453
Copy link

npdev453 commented Sep 24, 2023

Original issue:
sinclairzx81/typebox#601

I don't like when Record-like objects used in JSON-related things (API, Generators, Validators), basically, I think it because I have "childhood trauma" about JSONSchema that can't strictly declare left side of it.

Args:

  • Record-like objects closely woven in JS routine
  • no properly way to define its schema of key (like PositiveInt, format:'phone' & etc.)
  • no properly way to document it (title, description & etc.)
  • jsonschema -> ts (and I think others) generators can't properly generate key type, only indirectly
  • can be useful with additionalProperties: true
  • will be nice to have option to $ref that key

Input Examples:

const wasWindy = {
    '2018-11-12': true,
    '2018-11-13': false,
    ...
};
// [key: date] : boolean

const sensors = {
    '4c191b6b-fa2c-4aae-bb2a-26fb365c99b3': { active: true },
    'c8f20fd4-138f-4c50-84f9-2eff76300973': { active: false },
    ...
};  
// [key: UUID] : { active: boolean }
// or nominal: (that also can't be reproduced from schema)
// [key: SensorId] : SensorStatus

I don't see a clear way for now, but for the start:
Variant 1:

{ // some extra keys for left & right side
    type: "object",
    keys?: (string | number | sym) scheme, // or
    indexSignature?:  (string | number | sym) scheme, // maybe
    property?: (any) scheme,
    properties?: {}
}

Variant 2:

{ // new type 
    type: "record",
    key: (string | number | sym) scheme,
    value: ...
}

Variant 3:

{ // new key for left side
    type: "object",
    properties: {},
    additionalProperties: (any) scheme
    additionalKeys?: (string | number | sym) scheme,
}

Refs:
https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type
https://www.typescriptlang.org/docs/handbook/2/objects.html#index-signatures

@jdesrosiers
Copy link
Member

I'm not entirely following what you're asking for here, but you might be interested in the propertyNames keyword that allows you to define a schema that applies to all properties in an object. Here's a schema that would describe your first example.

{
  "type": "object",
  "description": "Was Windy Record",
  "patternProperties": {
    "": { "type": "boolean" }
  },
  "propertyNames": {
    "description": "The date it was windy",
    "$comment": "'type: string' is implicit because a property name isn't allowed to be anything else in JSON",
    "format": "date"
  }
}

@npdev453
Copy link
Author

Yeah, it is! Thanks!

Sorry for the panic, never seen it before (think because Swagger don't support this field)

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

No branches or pull requests

2 participants