Skip to content

Provide partial support for string "format" constraint #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions postgres-json-schema--0.1.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,28 @@ BEGIN
END IF;
END IF;

IF schema ? 'format' AND jsonb_typeof(data) = 'string' THEN
DECLARE
target text := (data #>> '{}');
EMAIL_RX constant text := '^[\w.!#$%&''*+/=?^`{|}~-]+@[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?(?:\.[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?)*$';
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Case sensitive check is faster, so why not use the full case sensitive regex here and target !~ EMAIL_RX below?

Suggested change
EMAIL_RX constant text := '^[\w.!#$%&''*+/=?^`{|}~-]+@[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?(?:\.[A-Z0-9](?:[A-Z0-9-]{0,61}[A-Z0-9])?)*$';
EMAIL_RX constant text := '^[a-zA-Z0-9.!#$%&''*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$';

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fine. Longer and maybe more difficult to read but 10% are always 10%.

BEGIN
CASE (schema->>'format')
WHEN 'date-time' THEN PERFORM target::timestamptz;
WHEN 'date' THEN PERFORM target::date;
WHEN 'time' THEN PERFORM target::time;
WHEN 'duration' THEN PERFORM target::interval;
WHEN 'uuid' THEN PERFORM target::uuid;
WHEN 'ipv6' THEN PERFORM target::inet; IF target NOT LIKE '%:%' THEN RAISE; END IF;
WHEN 'ipv4' THEN PERFORM target::inet; IF target LIKE '%:%' THEN RAISE; END IF;
WHEN 'regex' THEN PERFORM '' ~ target;
WHEN 'email' THEN IF target !~* EMAIL_RX THEN RAISE; END IF;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See regex comment above, a case sensitive check would be faster:

Suggested change
WHEN 'email' THEN IF target !~* EMAIL_RX THEN RAISE; END IF;
WHEN 'email' THEN IF target !~ EMAIL_RX THEN RAISE; END IF;

ELSE null; -- consistent with current behaviour - validate positive for unsupported options
END CASE;
EXCEPTION WHEN OTHERS THEN
RETURN false;
END;
END IF;

IF schema ? 'patternProperties' AND jsonb_typeof(data) = 'object' THEN
FOR prop IN SELECT jsonb_object_keys(data) LOOP
FOR pattern IN SELECT jsonb_object_keys(schema->'patternProperties') LOOP
Expand Down