0.0
No release in over 3 years
If you need to validate YAML against a schema, use this
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 5.15
~> 5.0
~> 13.0
 Project Readme

yaml-schema

A library for validating YAML documents against a schema.

Overview

yaml-schema is a schema validation library that allows you to enforce type safety and structural constraints on YAML files. It works directly with an AST (which can be obtained from Psych) representation to provide strict validation without implicit type coercion.

Usage

Basic Validation

require 'yaml-schema'

schema = {
  type: 'object',
  properties: {
    name: { type: 'string' },
    age: { type: 'integer' },
    active: { type: 'boolean' }
  }
}

yaml_string = <<~YAML
  name: "John Doe"
  age: 30
  active: true
YAML

node = Psych.parse(yaml_string)
YAMLSchema::Validator.validate(schema, node.children.first)

Supported Types

  • string - Scalar string values
  • integer - Numeric integers
  • boolean - Boolean values (true/false)
  • null - Null values
  • array - Sequences with optional constraints
    • items - Schema for all array elements
    • prefixItems - Fixed-position tuple validation
    • maxItems - Maximum array length
  • object - Mappings with properties
    • properties - Named properties with schemas
    • items - Schema for arbitrary key-value pairs
    • tag - Optional YAML tag requirement

Type Unions

Specify multiple valid types for a field:

schema = {
  type: 'object',
  properties: {
    value: { type: ['null', 'string'] }
  }
}

Nested Objects and Arrays

schema = {
  type: 'object',
  properties: {
    users: {
      type: 'array',
      items: {
        type: 'object',
        properties: {
          name: { type: 'string' },
          tags: {
            type: 'array',
            items: { type: 'string' }
          }
        }
      }
    }
  }
}

Error Handling

Validation errors include detailed path information:

begin
  YAMLSchema::Validator.validate(schema, node)
rescue YAMLSchema::Validator::UnexpectedType => e
  puts e.message  # e.g., "Expected integer at root -> age"
end

Error Types

  • UnexpectedType - Node doesn't match expected type
  • UnexpectedProperty - Object has unexpected properties
  • UnexpectedTag - YAML tag doesn't match schema
  • UnexpectedValue - Value doesn't match constraints
  • InvalidSchema - Schema definition is invalid