CheckMySchema

Structured data explained

JSON Schema Boolean vs. Enum: Test the Exact Values Your Field Accepts

Compare a boolean type, an enum of true and false, a named status enum and a constant with verified JSON fixtures.

    Two thin cards and two dark tokens sit near a laptop.
    Start with the two exact values a flag should allow.
    CheckMySchemaCheckMySchema

    At a glance

    The boolean type accepts true and false. An enum accepts exactly its listed values. Test strings, numbers, null and missing fields before choosing the contract.

    Read the general enum reference

    A JSON Schema boolean accepts the JSON values true and false. An enum accepts the exact members you list. Those rules are equivalent when the list holds just the two unquoted boolean values. They differ as soon as the list allows another value or permits only one of the two states.

    Write down the states the producer can actually send

    Suppose an import file has an optional approval field. If the source explicitly records a yes/no decision, use a boolean. If the source has named states such as pending, approved and declined, list those strings with enum. Before choosing the keyword, open a representative record or test fixture and check whether the value arrives as true or as the string “true.” They are separate JSON values.

    Use the validator for the draft your application supports. For a field that can stay pending, give that state an explicit meaning. A missing property, a property set to null and the text “pending” can be separate conditions. The schema should reflect the behavior the consumer expects from each.

    The unquoted JSON boolean true, quoted string "true" and numeric value 1 are displayed separately.
    Read the value as it arrives in the JSON data.
    Three blank slips are arranged on blue paper.
    Use separate named states when the data contract needs them.

    For documented examples of different types in an enum, see Enumerated Values Examples.

    Sources: Draft 2020-12 Validation Vocabulary · Enumerated Values Examples

    When a literal selects a whole record shape, test a version-tagged JSON Schema with two branches and check the field requirements for each version separately.

    Test a small set of exact values

    Try the same inputs against two narrow field schemas. The first uses a type of boolean. The second uses an enum containing true and false. Both allow those two literals and reject the text “true,” the number 1 and null. The equality check is about the actual JSON type and value. It does not interpret what an interface label says.

    Now use an enum of “pending,” “approved” and “declined.” These named strings should pass the status field. The literal true should fail, even if another field uses it for approval. Keep both examples in a regression test so the producer can change one field without accidentally changing the other.

    Both a boolean type and a two-member enum pass true and false and fail the quoted string "true".
    Add fixture failures alongside the examples you expect to pass.

    Two schemas can accept the same values

    Boolean type

    Allows true and false as JSON boolean values.

    Two-member enum

    An enum of true and false allows those same two exact values. A named status list accepts different values.

    Sources: Draft 2020-12 Validation Vocabulary · Enumerated Values Examples

    Keep property presence and allowed values separate

    A schema under properties validates the field when it is present. The required list is what makes the field mandatory. Test an object with no approval field and one with the field explicitly set to null. Decide whether either object should pass before adjusting the type.

    If you want only true, use const with that value. Include a negative example with false and another with the string “true.” That small set shows the difference between a boolean choice and one exact allowed value. If you need to migrate from string values to booleans, update the producer and consumer with the known cases and validate both sides of the change.

    Draft 2020-12 fixture outcomes without coercion.
    SchemaAcceptsRejects
    {"type":"boolean"}true, false"true", 1, null
    {"enum":[true,false]}true, false"true", 1, null
    {"enum":["pending","approved","declined"]}"pending", "approved", "declined"true, false, null
    {"const":true}truefalse, "true", null
    The matrix separates an absent property from one with null or true.
    Use required for existence and type or enum to test a value.

    Verify what the field accepts

    • Read the exact values from a fixture.
    • Test true and the string "true" separately.
    • Test null independently of property absence.
    • Check the producer and consumer in the same dialect.

    For the exact keyword definitions, see Draft 2020-12 Validation Vocabulary.

    For the related task, see Compare the JSON Schema types.

    Sources: JSON Schema

    Frequently asked questions

    Can an enum allow mixed JSON types?

    Yes. Without a separate type restriction, it can list values of different JSON types.

    Does a boolean schema accept the number 1?

    The literal 1 is a number. JSON Schema’s boolean type expects true or false.

    Which keyword allows just true?

    Use a constant whose value is true, or a single-member enum of the same value.

    Does optional mean null is valid?

    Optional controls whether the property must exist. Its value rules still apply when present.

    CheckMySchemaCheckMySchema

    Read how enum membership works

    Review mixed-type enumerations and named values.

    Read the general enum reference

    Continue with a related guide

    References