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

strictIndexSignatures option is ignored for single pattern properties #559

Open
yorinasub17 opened this issue Nov 18, 2023 · 0 comments · May be fixed by #560
Open

strictIndexSignatures option is ignored for single pattern properties #559

yorinasub17 opened this issue Nov 18, 2023 · 0 comments · May be fixed by #560

Comments

@yorinasub17
Copy link

yorinasub17 commented Nov 18, 2023

When generating for objects that have patternProperties with the strictIndexSignatures option, json-schema-to-typescript does not honor the strictIndexSignatures option.

I traced this down to this area of code:

if (schema.patternProperties) {
// partially support patternProperties. in the case that
// additionalProperties is not set, and there is only a single
// value definition, we can validate against that.
singlePatternProperty = !schema.additionalProperties && Object.keys(schema.patternProperties).length === 1
asts = asts.concat(
map(schema.patternProperties, (value, key: string) => {
const ast = parse(value, options, key, processed, usedNames)
const comment = `This interface was referenced by \`${parentSchemaName}\`'s JSON-Schema definition
via the \`patternProperty\` "${key}".`
ast.comment = ast.comment ? `${ast.comment}\n\n${comment}` : comment
return {
ast,
isPatternProperty: !singlePatternProperty,
isRequired: singlePatternProperty || includes(schema.required || [], key),
isUnreachableDefinition: false,
keyName: singlePatternProperty ? '[k: string]' : key,
}
}),
)

Where it returns the keyName as [k: string] in the singlePatternProperty case but doesn't update the keyName property of the ast, which is used by the strictIndexSignatures routine:

if (options.strictIndexSignatures && ast.keyName === '[k: string]') {
return `${type} | undefined`
}

I attempted a fix in this PR, here: #560

Example schema

{
  "type": "object",
  "properties": {
    "maybe": {
      "type": "string",
    },
    "pattern": {
      "type": "object",
      "properties": {
        "maybe": {
          "type": "string"
        }
      },
      "patternProperties": {
        "leaf|tree": {
          "type": "array"
        }
      }
    }
  }
}

Expected output

export interface Experiment {
  maybe?: string;
  pattern?: {
    maybe?: string;
    /**
     * This interface was referenced by `undefined`'s JSON-Schema definition
     * via the `patternProperty` "leaf|tree".
     */
    [k: string]: unknown[] | undefined;
  };
  [k: string]: unknown | undefined;
}

Actual output

export interface Experiment {
  maybe?: string;
  pattern?: {
    maybe?: string;
    /**
     * This interface was referenced by `undefined`'s JSON-Schema definition
     * via the `patternProperty` "leaf|tree".
     */
    [k: string]: unknown[];
  };
  [k: string]: unknown | undefined;
}

Related

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant