Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 39 ยท JSON Schema

JSON Schema Types and Constraints

Constrain values with JSON Schema: type, enum, const, numeric ranges, string length, regular expression patterns and format, with a runnable validator and clear error output.

The type keyword says what kind of value is allowed. Everything else in this lesson narrows the value further. Together these keywords catch the bulk of real-world data errors: wrong types, out-of-range numbers, empty strings and malformed codes.

type

The seven type names are string, number, integer, boolean, null, array and object. integer is a number with no fractional part, so 3 and 3.0 both pass. type also accepts an array to allow several types: ["string", "null"] means "a string or null".

Node.js type and type unions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

enum and const

  • enum: the value must equal one of the listed values.
  • const: the value must equal exactly one value.
JSON
{ "type": "string", "enum": ["draft", "published", "archived"] }

enum can list values of any type, even mixed ones, and objects are compared by deep equality.

Numbers

Keyword Meaning
minimum, maximum inclusive bounds
exclusiveMinimum, exclusiveMaximum exclusive bounds (a number in 2020-12)
multipleOf value must be a multiple, e.g. 0.01 for cents
Node.js Numeric constraints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Be careful with multipleOf and decimals: floating-point math makes 0.01 checks approximate, and real validators work around it differently. Integer cents with multipleOf: 1 avoids the problem.

Strings

Keyword Meaning
minLength, maxLength length in characters (Unicode code points)
pattern an ECMA-262 regular expression the string must match anywhere
format a named convention such as email, date, date-time, uri, uuid

pattern is not anchored. To require a complete match, write ^...$. In 2020-12, format is an annotation by default: validators may ignore it unless you enable format assertion. Treat it as documentation for people and do real checks in code or with pattern.

Node.js String constraints

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The last line passes because the validator counts code points, so one emoji has length 1.

default, title, description, examples

These are annotations: they never fail validation but help tools and readers. default documents the value an application uses when the field is absent (the validator does not insert it). examples is an array of sample values.

JSON
{
  "type": "integer",
  "title": "Page size",
  "description": "How many items one page holds",
  "default": 20,
  "minimum": 1,
  "maximum": 100,
  "examples": [10, 20, 50]
}

Putting it together

Node.js A product record

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which schema allows either a string or null?

    type accepts an array of type names.
  2. 2. Does the pattern keyword require the whole string to match?

    Patterns are not anchored, so add ^ and $ for a full match.
  3. 3. What does exclusiveMinimum 0 mean for a number?

    Exclusive bounds do not include the boundary value.
  4. 4. What is the role of the default keyword?

    default is an annotation; applications decide whether to apply it.

Summary

  • type (optionally a list) restricts the kind of value; enum and const fix the allowed values.
  • Numbers use minimum, maximum, exclusive bounds and multipleOf.
  • Strings use length limits, an unanchored pattern and the mostly advisory format.
  • Annotations such as title, description, default and examples never fail validation.

Next: Objects, arrays and tuples