Class: RuboCop::Cop::Obsession::Rails::FullyDefinedJsonField
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Obsession::Rails::FullyDefinedJsonField
- Defined in:
- lib/rubocop/cop/obsession/rails/fully_defined_json_field.rb
Overview
This cop checks for json(b) fields that are not fully defined with defaults or comments.
-
json(b) fields should have a default value like {} or [] so code can do my_field or my_field.first without fear that my_field is nil.
-
It is impossible to know the structure of a json(b) field just by reading the schema, because json(b) is an unstructured type. That’s why an “Example: …” Postgres comment should always be present when defining the field.
Instance Method Summary collapse
Instance Method Details
#on_send(node) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rubocop/cop/obsession/rails/fully_defined_json_field.rb', line 51 def on_send(node) return if !json_field?(node) = node.children[5] if !has_default?() add_offense(node, message: 'Add default value of {} or []') end if !has_comment_with_example?() add_offense( node, message: 'Add `comment: "Example: <example>"` option with an example array or hash value' ) end end |