Module: Senko

Defined in:
lib/senko.rb,
lib/senko/cache.rb,
lib/senko/errors.rb,
lib/senko/format.rb,
lib/senko/result.rb,
lib/senko/schema.rb,
lib/senko/dialect.rb,
lib/senko/version.rb,
lib/senko/compiler.rb,
lib/senko/validator.rb,
lib/senko/vocabulary.rb,
lib/senko/code_generator.rb,
lib/senko/compiler/optimizer.rb,
lib/senko/compiler/instruction.rb,
lib/senko/compiler/ref_resolver.rb,
ext/senko/senko.c

Defined Under Namespace

Modules: Dialect, Format, Instructions, Native Classes: Cache, CircularReferenceError, CodeGenerator, CompileError, Compiler, Error, Instruction, Result, Schema, UnresolvableRefError, UnsupportedDialectError, ValidationError, Validator, Vocabulary

Constant Summary collapse

GLOBAL_CACHE =
Cache.new
GLOBAL_FORMATS =
{}
GLOBAL_KEYWORDS =
{}
UNSET =
Object.new
VERSION =
'0.1.0'

Class Method Summary collapse

Class Method Details

.clear_cache!Object



58
59
60
61
# File 'lib/senko.rb', line 58

def clear_cache!
  GLOBAL_CACHE.clear
  nil
end

.clear_registry!Object



79
80
81
82
83
# File 'lib/senko.rb', line 79

def clear_registry!
  GLOBAL_FORMATS.clear
  GLOBAL_KEYWORDS.clear
  touch_registry!
end

.compile(schema = UNSET, **options) ⇒ Object



27
28
29
30
31
32
33
34
35
36
# File 'lib/senko.rb', line 27

def compile(schema = UNSET, **options)
  schema, options = split_schema_keywords(options) if schema.equal?(UNSET)

  merged_options = merged_options(options)
  if cacheable?(schema, merged_options)
    GLOBAL_CACHE.fetch(cache_key(schema, merged_options)) { build_schema(schema, merged_options) }
  else
    build_schema(schema, merged_options)
  end
end

.compile_file(path, **options) ⇒ Object



38
39
40
# File 'lib/senko.rb', line 38

def compile_file(path, **options)
  compile(JSON.parse(File.read(path)), **options)
end

.register_format(name, callable = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


63
64
65
66
67
68
69
# File 'lib/senko.rb', line 63

def register_format(name, callable = nil, &block)
  validator = callable || block
  raise ArgumentError, 'format validator must respond to call' unless validator.respond_to?(:call)

  GLOBAL_FORMATS[name.to_s] = validator
  touch_registry!
end

.register_keyword(name, callable = nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


71
72
73
74
75
76
77
# File 'lib/senko.rb', line 71

def register_keyword(name, callable = nil, &block)
  validator = callable || block
  raise ArgumentError, 'keyword validator must respond to call' unless validator.respond_to?(:call)

  GLOBAL_KEYWORDS[name.to_s] = validator
  touch_registry!
end

.registry_versionObject



85
86
87
# File 'lib/senko.rb', line 85

def registry_version
  @registry_version
end

.valid?(schema, data, **options) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/senko.rb', line 42

def valid?(schema, data, **options)
  compile(schema, **options).valid?(data)
end

.valid_json?(schema, json, **options) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/senko.rb', line 50

def valid_json?(schema, json, **options)
  compile(schema, **options).valid_json?(json)
end

.validate(schema, data, **options) ⇒ Object



46
47
48
# File 'lib/senko.rb', line 46

def validate(schema, data, **options)
  compile(schema, **options).validate(data)
end

.validate_json(schema, json, **options) ⇒ Object



54
55
56
# File 'lib/senko.rb', line 54

def validate_json(schema, json, **options)
  compile(schema, **options).validate_json(json)
end