FAQ

  • GitHub GitHub Repo stars
  • Discord Discord Server
  • ✨ New! Try the OpenAPI Doctor ✨ The OpenAPI Doctor

    schema


    schema is a function that takes a JSON Schema definition, and validates that an object looked up via JSON Path matches that schema. If the object does not exist, the function will trigger a rule violation

    How do I use this function?

    This function is configured by the following functionOptions

    NAME TYPE REQUIRED? DESCRIPTION
    schema JSON Schema yes The schema you want to evaluate
    forceValidationOnCurrentNode boolean no Validate the current node directly instead of looking for a field (default: false, auto-enabled for root validation with no field)

    Important Notes

    • When validating non-OpenAPI documents, use the --skip-check flag with the vacuum CLI
    • When given is "$" (root) and no field is specified, the function automatically validates the entire document against the schema (equivalent to forceValidationOnCurrentNode: true)
    • This automatic behavior ensures consistency with Spectral when validating entire documents

    Example ruleset configuration

    openapi-tags:
      description: Global tags should exist, and be an array
      type: validation
      recommended: false
      given: "$"
      then:
        field: "tags"
        function: "schema"
        functionOptions:
          schema:
            type: "array"
            items:
              type: "object"
              minItems: 1
            uniqueItems: true
    

    Example for validating non-OpenAPI documents

    When validating entire non-OpenAPI YAML/JSON documents (e.g., configuration files), the schema function will automatically validate the root node when no field is specified:

    # Rule to ensure a configuration file only contains allowed properties
    config-validation:
      description: Ensure only valid properties in config file
      severity: error
      given: "$"  # Match the root
      then:
        # No field specified - validates entire document
        function: "schema"
        functionOptions:
          schema:
            type: object
            properties:
              consumers:
                type: array
                items:
                  type: object
                  properties:
                    name:
                      type: string
                    id:
                      type: string
                  required: [name, id]
            additionalProperties: false  # Reject any properties not defined above
    

    To lint a non-OpenAPI document with this rule:

    vacuum lint config.yaml -r rules.yaml --skip-check
    

    View Spectral Equivalent


    View Function Source



    What's on this page