Generating type interfaces without additional properties

We are attempting to set up Snowtype for a data structure, but we have encountered an issue where the generated TypeScript interface is not adhering to the rules defined in the schema.According to the JSON Schema we provided (see below), additional properties should not be allowed in the data structure. However, when generating the TypeScript interface using Snowtype, we noticed that the interface includes an index signature [property: string]: any; for the Tv2PlayProduct, Feature, and Pricing interfaces. This index signature allows any additional properties to be added to the objects, which contradicts what is allowed in the console.Here is the relevant part of the generated TypeScript interface:

typescript

export interface Tv2PlayProduct {
    disney?:                   string;
    features?:                 Feature[];
    hayu?:                     boolean;
    hvodAlternativeProductId?: string;
    id:                        string;
    includedAddOns?:           string[];
    isActive?:                 boolean;
    name:                      string;
    pricing?:                  Pricing[];
    productType:               string;
    supportedAddOns?:          string[];
    [property: string]: any;
}

export interface Feature {
    id:    string;
    name?: string;
    [property: string]: any;
}

export interface Pricing {
    campaign?:      string;
    initialPrice:   number;
    period?:        string;
    periodId:       PeriodID;
    recurringPrice: number;
    [property: string]: any;
}

The JSON Schema we are using is as follows:

{
  "$schema": "http://iglucentral.com/schemas/com.snowplowanalytics.self-desc/schema/jsonschema/1-0-0#",
  "type": "object",
  "self": {
    "format": "jsonschema",
    "version": "2-0-0"
  },
  "description": "Schema for a product which can be purchased",
  "properties": {
    "name": {
      "type": "string"
    },
    "id": {
      "type": "string"
    },
    "productType": {
      "type": "string"
    },
    "includedAddOns": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "supportedAddOns": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "hayu": {
      "type": "boolean"
    },
    "pricing": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "initialPrice",
          "recurringPrice",
          "periodId"
        ],
        "properties": {
          "initialPrice": {
            "type": "number"
          },
          "recurringPrice": {
            "type": "number"
          },
          "periodId": {
            "type": "string",
            "enum": [
              "h_48",
              "day",
              "week",
              "month",
              "year",
              "halfyear",
              "est",
              "unknown"
            ]
          },
          "period": {
            "type": "string"
          },
          "campaign": {
            "type": "string"
          }
        }
      }
    },
    "isActive": {
      "type": "boolean"
    },
    "disney": {
      "type": "string"
    },
    "features": {
      "type": "array",
      "items": {
        "type": "object",
        "required": [
          "id"
        ],
        "properties": {
          "name": {
            "type": "string"
          },
          "id": {
            "type": "string"
          }
        }
      }
    },
    "hvodAlternativeProductId": {
      "type": "string"
    }
  },
  "required": [
    "name",
    "id",
    "productType"
  ]
}

console:

snowtype is technically following the spec:

If “additionalProperties” is absent, it may be considered present with an empty schema as a value.

So additionalProperties essentially is supposed to default to true, but for the console’s linting purposes it essentially treats it as defaulting to false (because of the issues noted in the docs here) unless you explicitly opt-in to additionalProperties: true or similar, because that’s what most pipelines would need to correctly capture data. The pipeline itself also assumes additionalProperties is true unless specified, so the console is the outlier here.

Perhaps that warning should be about the missing/ambiguous additionalProperties, rather than presuming it should be false. Either way I think always being explicit about additionalProperties in your schema definitions is the way to go in future.

1 Like