Kwalify 0.7.1 released

I have released Kwalify 0.7.1. http://www.kuwata-lab.com/kwalify/

Kwalify is a schema validator for YAML and JSON. From this version, YAML parser is rewrited from scratch and is integrated with data binding.

Here is the enhancements and changes.

Enhancements:

YAML parser is rewrited from scratch. The new parser class Kwalify::Yaml::Parser is available. The old parser class Kwalify::YamlParser is stil available, but it is not recommended to use.

Validator is integrated with yaml parser. It is able to parse and validate at once.

## create validator
require 'kwalify'
schema = Kwalify::Yaml.load_file('schema.yaml')
validator = Kwalify::Validator.new(schema)
## parse with validation
parser = Kwalify::Yaml::Parser.new(validator)
ydoc = parser.parse(File.read('data.yaml'))
## show validation errors if exist
errors = parser.errors()
if errors && !errors.empty?
  for e in errors
    puts "** %d:%d [%s] %s" % [e.linenum, e.column, e.path, e.message] 
  end
end

Data binding is integrated into Kwalify::Yaml::Parser. If you set Kwalify::Yaml::Parser#data_binding to true and you specified class names in schema file, parser creates instance objects instead of Hash objects. It means that you don’t need to add ‘!ruby/Classname’ for each data.

schema file (config.schema.yaml)

type:  map
class: Config
mapping:
 "host": { type: str, required: true }
 "port": { type: int }
 "user": { type: str, required: true }
 "pass": { type: str, required: true }

configuration file (config.yaml)

host:  localhost
port:  8080
user:  user1
pass:  password1

ruby program (ex1.rb)

## class definition
require 'kwalify/util/hashlike'
class Config
  include Kwalify::Util::HashLike  # defines [], []=, ...
  attr_accessor :host, :posrt, :user, :pass
end
## create validator object
require 'kwalify'
schema = Kwalify::Yaml.load_file('config.schema.yaml')
validator = Kwalify::Validator.new(schema)
## parse configuration file with data binding
parser = Kwalify::Yaml::Parser.new(validator)
parser.data_binding = true    # enable data binding
config = parser.parse_file('config.yaml')
p config  #=> #<Config:0x542590 @user="user1", @port=8080,
          #        @pass="password1", @host="localhost">

Preceding alias supported. If you set Kwalify::Yaml::Parser#preceding_alias to true, parser allows aliases to apprear before corresponding anchor appears. This is very useful when node graph is complex.

- name: Foo
  parent: *bar          # preceding alias
- &bar
  name: Bar
  parent: *baz          # preceding alias
- &baz
  name: Baz

New command-line option ‘-P’ enables preceding alias.

Kwalify::Yaml.load() and Kwalify::Yaml.load_file() are added. They are similar to YAML.load() and YAML.load_file() but they use Kwalify::Yaml::Parser object.

New utilify method Kwalify::Util.traverse_schema() provided.

require 'kwalify'
require 'kwalify/util'
schema = Kwalify::Yaml.load('schema.yaml')
Kwalify::Util.traverse_schema(schema) do |rulehash|
  if classname = rulehash['class']
    ## add namespace to class name
    rulehash['class'] = "Foo::Bar::#{classname}"
  end
end

Add ‘kwalify/util/hashlike.rb’ which contains definition of Kwalify::Util::HashLike module. This module defines [], []=, keys(), key?(), and each() methods, and these are required for data-binding.

Action ‘genclass-ruby’ supports ‘–hashlike’ property.

Action ‘genclass-ruby’ supports ‘–initialize=false’ property.

Add action ‘geclass-php’.

‘\xXX’ and ‘\uXXXX’ are supported in Kwalify::Yaml::Parser.

New constrant ‘default:’ is added to kwalify.schema.yaml. This constrant have no effect to validation and parsing, and it is used only in ‘genclass-xxx’ action.

Leave a reply