Validate not nil, min and max for a string

The following validation code doesn't work:
attributes do
uuid_primary_key :id

attribute :content, :string do
allow_nil? false
constraints: [min_length: 1, max_length: 255]
end
end
attributes do
uuid_primary_key :id

attribute :content, :string do
allow_nil? false
constraints: [min_length: 1, max_length: 255]
end
end
In the documentation I can only find examples for allow_nil? or constraints: [min_length: 1, max_length: 255] but none where both are combined. How do I have to change the code to work?
4 Replies
ZachDaniel
ZachDaniel2y ago
constraints [min_length: 1, max_length: 255]
constraints [min_length: 1, max_length: 255]
Stefan Wintermeyer
How can I use the do ... end syntax with this validation?
attribute :priority, :integer, allow_nil?: true, constraints: [min: 1, max: 3]
attribute :priority, :integer, allow_nil?: true, constraints: [min: 1, max: 3]
barnabasj
barnabasj2y ago
the do end is an alternative syntax, you have the required arguments first and then the options as a keyword list last. instead of passing it as a keyword list you can also use the do end block.
attribute :priority, :integer do
allow_nil? true
constraints [min: 1, max: 3]
end
attribute :priority, :integer do
allow_nil? true
constraints [min: 1, max: 3]
end
Stefan Wintermeyer
Thanks @barnabasj !

Did you find this page helpful?