Class: RenderEditorjs::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/render_editorjs/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, schema) ⇒ Validator

Returns a new instance of Validator.



7
8
9
10
11
12
# File 'lib/render_editorjs/validator.rb', line 7

def initialize(data, schema)
  @data = data
  @schema = schema

  @errors = []
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/render_editorjs/validator.rb', line 5

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/render_editorjs/validator.rb', line 5

def errors
  @errors
end

#schemaObject (readonly)

Returns the value of attribute schema.



5
6
7
# File 'lib/render_editorjs/validator.rb', line 5

def schema
  @schema
end

Instance Method Details

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
# File 'lib/render_editorjs/validator.rb', line 14

def valid?
  validate!
rescue JSON::Schema::ValidationError => e
  @errors << e.message

  false
end

#validate!Object



22
23
24
# File 'lib/render_editorjs/validator.rb', line 22

def validate!
  JSON::Validator.validate!(schema, data)
end