Validate format of subdomains / schema names

Note we're using the `:HOST` regular expression since subdomains can
contain the same characters as domains do. This isn't 100% precise,
though, since subdomains have a maximum length of 63 characters, but is
good enough for our purposes.
This commit is contained in:
Javi Martín
2022-09-25 16:48:23 +02:00
parent c483c6036a
commit d77cf77761
2 changed files with 12 additions and 1 deletions

View File

@@ -2,7 +2,8 @@ class Tenant < ApplicationRecord
validates :schema, validates :schema,
presence: true, presence: true,
uniqueness: true, uniqueness: true,
exclusion: { in: ->(*) { excluded_subdomains }} exclusion: { in: ->(*) { excluded_subdomains }},
format: { with: URI::DEFAULT_PARSER.regexp[:HOST] }
validates :name, presence: true, uniqueness: true validates :name, presence: true, uniqueness: true
after_create :create_schema after_create :create_schema

View File

@@ -25,6 +25,16 @@ describe Tenant do
end end
end end
it "is valid with nested subdomains" do
tenant.schema = "multiple.sub.domains"
expect(tenant).to be_valid
end
it "is not valid with an invalid subdomain" do
tenant.schema = "my sub domain"
expect(tenant).not_to be_valid
end
it "is not valid without a name" do it "is not valid without a name" do
tenant.name = "" tenant.name = ""
expect(tenant).not_to be_valid expect(tenant).not_to be_valid