Objects
Welcome to the Objects module. In this module, you will learn all about the object type and constraints that can be applied to it in JSON Schema.
Pattern Properties
So far, we have seen how to define the properties of an object in a schema. Sometimes you want to say that, given a particular kind of property name, the value should match a particular schema.
That's where patternProperties comes in: it maps regular expressions to schemas. If a property name matches the given regular expression, the property value must validate against the corresponding schema.
1{ 2 "name": "John Doe", 3 "age": 25, 4 "DEPT-001": "HR" 5}
We will define a property that starts with DEPT- followed by department number of length 3 and the value should be a string which is department name.
Example:
1{ 2 "type":"object", 3 "patternProperties": { 4 "^DEPT-[0-9]{3}$": { 5 "type": "string" 6 } 7 } 8}
Now, modify the schema provided in the side editor on the right to permit DEPT-* properties. Additionally, ensure that the values for DEPT-* properties are constrained to be in all capital letters.
Hint: Use the patternProperties keyword to define the pattern for the property name and pattern keyword to define the pattern for the property value. you can set pattern to ^[A-Z]+$ to match all capital letters.
We have set additionalProperties to false to disallow any additional properties. We will learn more about additionalProperties in the next lesson.