-
Notifications
You must be signed in to change notification settings - Fork 144
/
email.sql
34 lines (27 loc) · 1.44 KB
/
email.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
CREATE EXTENSION IF NOT EXISTS citext;
/*
Why base type is citext and not text?
Because email is case insensetive, for example: [email protected] = [email protected].
So we can create unique constraint `alter table my_table add unique (email)`. Not `alter table my_table add unique (lower(email))`.
*/
CREATE DOMAIN public.email AS public.citext CHECK(
octet_length(VALUE) BETWEEN 6 AND 320 -- https://en.wikipedia.org/wiki/Email_address
AND VALUE LIKE '_%@_%.__%' -- rough, but quick check email syntax
--AND public.is_email(VALUE) -- accurate, but very slow check email syntax, so don't use it in domain!
);
COMMENT ON DOMAIN public.email IS 'Aдрес электронной почты с минимальной, но быстрой валидацией';
--Полная, но относительно медленная валидация email почти по спецификации:
--https://github.com/rin-nas/postgresql-patterns-library#Как-провалидировать-значение-поля-только-если-оно-явно-указано-в-UPDATE-запросе
--TEST
do $$
begin
assert null::public.email is null;
assert '[email protected]'::public.email is not null;
end
$$;
do $$
BEGIN
assert 'e@m.'::public.email is not null ; --raise exception [23514] ERROR: value for domain email violates check constraint "email_check"
EXCEPTION WHEN SQLSTATE '23514' THEN
END;
$$;