Skip to content

Commit b9f03ef

Browse files
authored
Merge pull request #12 from launchql/fix/attchment-in-types
fixed the logic in attachment in types
2 parents 880c1b9 + f5049cd commit b9f03ef

5 files changed

Lines changed: 54 additions & 38 deletions

File tree

packages/data-types/types/__tests__/domains.pgutils.test.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ const invalidUrls = [
4444
];
4545

4646
const validAttachments = [
47+
'http://www.foo.bar/some.jpg',
48+
'https://foo.bar/some.PNG'
49+
];
50+
51+
const invalidAttachments = [
52+
'hi there',
53+
'ftp://foo.bar/some.png',
54+
'https:///foo.bar/some.png'
55+
];
56+
57+
const validImages = [
4758
{ url: 'http://www.foo.bar/some.jpg', mime: 'image/jpg' },
4859
{ url: 'https://foo.bar/some.PNG', mime: 'image/jpg' }
4960
];
5061

51-
const invalidAttachments = [
62+
const invalidImages = [
5263
{ url: 'hi there' },
5364
{ url: 'https://foo.bar/some.png' }
5465
];
@@ -87,24 +98,30 @@ afterAll(async () => {
8798

8899
describe('types', () => {
89100
it('valid attachment and image', async () => {
90-
for (const value of validAttachments) {
91-
await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [value]);
92-
await pg.any(`INSERT INTO customers (attachment) VALUES ($1::json);`, [value]);
101+
for (const attachment of validAttachments) {
102+
await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [attachment]);
103+
}
104+
105+
for (const image of validImages) {
106+
await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]);
93107
}
94108
});
95109

96110
it('invalid attachment and image', async () => {
97-
for (const value of invalidAttachments) {
111+
for (const attachment of invalidAttachments) {
98112
let failed = false;
99113
try {
100-
await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [value]);
114+
await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [attachment]);
101115
} catch (e) {
102116
failed = true;
103117
}
104118
expect(failed).toBe(true);
105-
failed = false;
119+
}
120+
121+
for (const image of invalidImages) {
122+
let failed = false;
106123
try {
107-
await pg.any(`INSERT INTO customers (image) VALUES ($1);`, [value]);
124+
await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]);
108125
} catch (e) {
109126
failed = true;
110127
}

packages/data-types/types/__tests__/domains.test.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ const invalidUrls = [
4444
];
4545

4646
const validAttachments = [
47+
'http://www.foo.bar/some.jpg',
48+
'https://foo.bar/some.PNG'
49+
];
50+
51+
const invalidAttachments = [
52+
'hi there',
53+
'ftp://foo.bar/some.png',
54+
'https:///foo.bar/some.png'
55+
];
56+
57+
const validImages = [
4758
{ url: 'http://www.foo.bar/some.jpg', mime: 'image/jpg' },
4859
{ url: 'https://foo.bar/some.PNG', mime: 'image/jpg' }
4960
];
5061

51-
const invalidAttachments = [
62+
const invalidImages = [
5263
{ url: 'hi there' },
5364
{ url: 'https://foo.bar/some.png' }
5465
];
@@ -87,24 +98,30 @@ afterAll(async () => {
8798

8899
describe('types', () => {
89100
it('valid attachment and image', async () => {
90-
for (const value of validAttachments) {
91-
await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [value]);
92-
await pg.any(`INSERT INTO customers (attachment) VALUES ($1::json);`, [value]);
101+
for (const attachment of validAttachments) {
102+
await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [attachment]);
103+
}
104+
105+
for (const image of validImages) {
106+
await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]);
93107
}
94108
});
95109

96110
it('invalid attachment and image', async () => {
97-
for (const value of invalidAttachments) {
111+
for (const attachment of invalidAttachments) {
98112
let failed = false;
99113
try {
100-
await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [value]);
114+
await pg.any(`INSERT INTO customers (attachment) VALUES ($1);`, [attachment]);
101115
} catch (e) {
102116
failed = true;
103117
}
104118
expect(failed).toBe(true);
105-
failed = false;
119+
}
120+
121+
for (const image of invalidImages) {
122+
let failed = false;
106123
try {
107-
await pg.any(`INSERT INTO customers (image) VALUES ($1);`, [value]);
124+
await pg.any(`INSERT INTO customers (image) VALUES ($1::json);`, [image]);
108125
} catch (e) {
109126
failed = true;
110127
}

packages/data-types/types/deploy/schemas/public/domains/attachment.sql

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22
-- requires: schemas/public/schema
33

44
BEGIN;
5-
CREATE DOMAIN attachment AS jsonb CHECK (
6-
(
7-
jsonb_typeof(VALUE) = 'object'
8-
AND VALUE ?& ARRAY['url', 'mime']
9-
AND VALUE->>'url' ~ '^(https?)://[^\s/$.?#].[^\s]*$'
10-
)
11-
OR (
12-
jsonb_typeof(VALUE) = 'string'
13-
AND VALUE #>> '{}' ~ '^(https?)://[^\s/$.?#].[^\s]*$'
14-
)
15-
);
5+
CREATE DOMAIN attachment AS text CHECK (VALUE ~ '^(https?)://[^\s/$.?#].[^\s]*$');
166
COMMENT ON DOMAIN attachment IS E'@name launchqlInternalTypeAttachment';
177
COMMIT;

packages/utils/faker/deploy/schemas/faker/procedures/utils.sql

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -724,15 +724,11 @@ $$
724724
LANGUAGE 'plpgsql' VOLATILE;
725725

726726
CREATE FUNCTION faker.attachment(mime text default null) returns attachment as $$
727-
DECLARE
728-
obj jsonb = '{}'::jsonb;
729727
BEGIN
730728
IF (mime IS NULL) THEN
731729
mime = faker.mime();
732730
END IF;
733-
obj = jsonb_set(obj, '{url}', to_jsonb(faker.url(mime)::text));
734-
obj = jsonb_set(obj, '{mime}', to_jsonb(mime));
735-
RETURN obj;
731+
RETURN faker.url(mime);
736732
END;
737733
$$
738734
LANGUAGE 'plpgsql' VOLATILE;

packages/utils/faker/sql/launchql-faker--0.9.0.sql

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -677,15 +677,11 @@ END;
677677
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
678678

679679
CREATE FUNCTION faker.attachment(mime text DEFAULT NULL) RETURNS attachment AS $EOFCODE$
680-
DECLARE
681-
obj jsonb = '{}'::jsonb;
682680
BEGIN
683681
IF (mime IS NULL) THEN
684682
mime = faker.mime();
685683
END IF;
686-
obj = jsonb_set(obj, '{url}', to_jsonb(faker.url(mime)::text));
687-
obj = jsonb_set(obj, '{mime}', to_jsonb(mime));
688-
RETURN obj;
684+
RETURN faker.url(mime);
689685
END;
690686
$EOFCODE$ LANGUAGE plpgsql VOLATILE;
691687

@@ -10405,4 +10401,4 @@ INSERT INTO faker.dictionary (
1040510401
('cta', 'Learn more'),
1040610402
('cta', 'Join now'),
1040710403
('cta', 'Start now'),
10408-
('cta', 'Get yours now');
10404+
('cta', 'Get yours now');

0 commit comments

Comments
 (0)