JSON Schema propertyNames doesnt seem to work

i am trying to use a schema to validate some input data in an ionic WebApp. But it doesnt seem to work the way i want it to. Schema and example input data here: https://www.jsonschemavalidator.net/s/ukPB5SkA.

specifically the object should be allowed to contain properties “header” “footer” “color” and “typeface”. Yet other properties seem to be allowed despite additionalProperties being set to false.

I also tired adding:

    "patternProperties": {
        ".*": "string"
    }

i use ajv in my project so i tried using a format:

import addFormats from 'ajv-formats';
import Ajv2019 from "ajv/dist/2019"
const ajv = new Ajv2019({strict: true, removeAdditional: 'all',  allowUnionTypes: true, passContext: true})
addFormats(ajv)
ajv.addFormat("collection", {
  type: "string",
  validate: (x) => Object.keys(Properties).includes(x)
})

enum Properties {
  'header',
  'footer',
  'color',
  'typeface'
}

const complexSchemaFormat = {
  "type": "object",
  "properties": {
    "propertyNames": {
      "type": "string",
      "format": "collection"
    },
    "additionalProperties": false
  }
} as const

    const complexInput2 = {
      "jiii": 2,
      "juuu": "laal"
    }
  protected doThing() {
    const complexIsValid3 = ajv.validate(complexSchemaFormat,complexInput2)
    console.log(complexIsValid3) // is true, should be false
    }

Alternative from the previous answer

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "additionalProperties": false,
  "type": "object",
  "properties": {
    "header": {"type": "string"},
    "footer": {"type": "string"},
    "color": {"type": "string"},
    "typeface": {"type": "string"}
  }
}

propertyNames is an applicator similar to properties.
The description of this keyword states:

The propertyNames keyword in is used to define constraints on the property names within an object instance. It allows you to specify a schema that all the property names in an object instance must adhere to.

The way you have written your schema, the actual property name defined is propertyNames.

{
  "propertyNames": "something"
}

It should be something like this where you want to constraint the naming to your enum. BUT, this doesn’t give you way to constraint the values of those properties.

{
"$schema": "https://json-schema.org/draft/2019-09/schema",
"propertyNames": {
   "pattern": "header|footer|color|typeface"
  }
}

You probably really want to do something like this where you don’t even need to define a custom format. JSON Schema validation will validate what you are trying to do with the custom format with the following

{
  "$schema": "https://json-schema.org/draft/2019-09/schema",
  "unevaluatedProperties": false,
  "patternProperties": {
    "header|footer|color|typeface": {
      "type": "string"
    }
  }
}

This basically says, any property defined in this schema must be one of the values in patternProperties and then it must validate as a string

3

I found a suitable solution. I put the enums into separate schemas and then used these subschemas to

  1. have only the propertyNames allowed that are keys of an ts enum
  2. have an array property “flags” allowed that can only have the listed properties as members.
    This makes the schema much longer and kind of convoluted but it does what i want it to do:
export const PropertiesSchema = {
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$ref": "#/definitions/Properties",
  "definitions": {
    "Properties": {
      "type": "object",
      "properties": {
        "flags": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Flags"
          }
        }
      },
        "propertyNames": {
        "$ref": "#/definitions/StyleParams"
      },
    },
    "StyleParams": {
      "type": "string",
      "enum": [
        "flags",
        ...Object.keys(properties) // ts enum
      ]
    },
    "Flags": {
      "type": "string",
      "enum": [
        classconstants.Constant1,
        classconstants.Constant2,
        classconstants.Constant3
      ]
    }
  }
} as const

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật