Validate belongs_to exists

How can I validate that for the following resource the level_id is not blank and that the Level resource for that level_id exists?

attributes do
  uuid_primary_key :id

  attribute :name, :string do
    allow_nil? false
    constraints max_length: 255
  end

  attribute :code, :string, constraints: [max_length: 3]

  attribute :slug, :string do
    allow_nil? false
    constraints max_length: 255
  end

  create_timestamp :inserted_at
  update_timestamp :updated_at
end

validations do
  validate present([:name, :slug])
end

relationships do
  belongs_to :level, Feriendaten.Geo.Level do
    attribute_writable? true
  end
end
Was this page helpful?