diff --git a/docker-compose/ext-src/pg_jsonschema-src/Makefile b/docker-compose/ext-src/pg_jsonschema-src/Makefile new file mode 100644 index 0000000000..d79364d8b5 --- /dev/null +++ b/docker-compose/ext-src/pg_jsonschema-src/Makefile @@ -0,0 +1,8 @@ +EXTENSION = pg_jsonschema +DATA = pg_jsonschema--1.0.sql +REGRESS = jsonschema_valid_api jsonschema_edge_cases +REGRESS_OPTS = --load-extension=pg_jsonschema + +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) diff --git a/docker-compose/ext-src/pg_jsonschema-src/expected/jsonschema_edge_cases.out b/docker-compose/ext-src/pg_jsonschema-src/expected/jsonschema_edge_cases.out new file mode 100644 index 0000000000..f4089bfb13 --- /dev/null +++ b/docker-compose/ext-src/pg_jsonschema-src/expected/jsonschema_edge_cases.out @@ -0,0 +1,87 @@ +-- Schema with enums, nulls, extra properties disallowed +SELECT jsonschema_is_valid('{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false +}'::json); + jsonschema_is_valid +--------------------- + t +(1 row) + +-- Valid enum and null email +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "active", "email": null}'::json +); + jsonschema_validation_errors +------------------------------ + {} +(1 row) + +-- Invalid enum value +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "disabled", "email": null}'::json +); + jsonschema_validation_errors +---------------------------------------------------------------------- + {"\"disabled\" is not one of [\"active\",\"inactive\",\"pending\"]"} +(1 row) + +-- Invalid email format (assuming format is validated) +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "active", "email": "not-an-email"}'::json +); + jsonschema_validation_errors +----------------------------------------- + {"\"not-an-email\" is not a \"email\""} +(1 row) + +-- Extra property not allowed +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "active", "extra": "should not be here"}'::json +); + jsonschema_validation_errors +-------------------------------------------------------------------- + {"Additional properties are not allowed ('extra' was unexpected)"} +(1 row) + diff --git a/docker-compose/ext-src/pg_jsonschema-src/expected/jsonschema_valid_api.out b/docker-compose/ext-src/pg_jsonschema-src/expected/jsonschema_valid_api.out new file mode 100644 index 0000000000..73f0a562e7 --- /dev/null +++ b/docker-compose/ext-src/pg_jsonschema-src/expected/jsonschema_valid_api.out @@ -0,0 +1,65 @@ +-- Define schema +SELECT jsonschema_is_valid('{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] +}'::json); + jsonschema_is_valid +--------------------- + t +(1 row) + +-- Valid instance +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] + }'::json, + '{"username": "alice", "age": 25}'::json +); + jsonschema_validation_errors +------------------------------ + {} +(1 row) + +-- Invalid instance: missing required "username" +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] + }'::json, + '{"age": 25}'::json +); + jsonschema_validation_errors +----------------------------------------- + {"\"username\" is a required property"} +(1 row) + +-- Invalid instance: wrong type for "age" +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] + }'::json, + '{"username": "bob", "age": "twenty"}'::json +); + jsonschema_validation_errors +------------------------------------------- + {"\"twenty\" is not of type \"integer\""} +(1 row) + diff --git a/docker-compose/ext-src/pg_jsonschema-src/sql/jsonschema_edge_cases.sql b/docker-compose/ext-src/pg_jsonschema-src/sql/jsonschema_edge_cases.sql new file mode 100644 index 0000000000..edad8cca16 --- /dev/null +++ b/docker-compose/ext-src/pg_jsonschema-src/sql/jsonschema_edge_cases.sql @@ -0,0 +1,66 @@ +-- Schema with enums, nulls, extra properties disallowed +SELECT jsonschema_is_valid('{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false +}'::json); + +-- Valid enum and null email +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "active", "email": null}'::json +); + +-- Invalid enum value +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "disabled", "email": null}'::json +); + +-- Invalid email format (assuming format is validated) +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "active", "email": "not-an-email"}'::json +); + +-- Extra property not allowed +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "status": { "type": "string", "enum": ["active", "inactive", "pending"] }, + "email": { "type": ["string", "null"], "format": "email" } + }, + "required": ["status"], + "additionalProperties": false + }'::json, + '{"status": "active", "extra": "should not be here"}'::json +); diff --git a/docker-compose/ext-src/pg_jsonschema-src/sql/jsonschema_valid_api.sql b/docker-compose/ext-src/pg_jsonschema-src/sql/jsonschema_valid_api.sql new file mode 100644 index 0000000000..44539ed6ce --- /dev/null +++ b/docker-compose/ext-src/pg_jsonschema-src/sql/jsonschema_valid_api.sql @@ -0,0 +1,48 @@ +-- Define schema +SELECT jsonschema_is_valid('{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] +}'::json); + +-- Valid instance +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] + }'::json, + '{"username": "alice", "age": 25}'::json +); + +-- Invalid instance: missing required "username" +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] + }'::json, + '{"age": 25}'::json +); + +-- Invalid instance: wrong type for "age" +SELECT jsonschema_validation_errors( + '{ + "type": "object", + "properties": { + "username": { "type": "string" }, + "age": { "type": "integer" } + }, + "required": ["username"] + }'::json, + '{"username": "bob", "age": "twenty"}'::json +); diff --git a/docker-compose/ext-src/pg_session_jwt-src/Makefile b/docker-compose/ext-src/pg_session_jwt-src/Makefile new file mode 100644 index 0000000000..c61c9777ad --- /dev/null +++ b/docker-compose/ext-src/pg_session_jwt-src/Makefile @@ -0,0 +1,9 @@ +EXTENSION = pg_session_jwt + +REGRESS = basic_functions +REGRESS_OPTS = --load-extension=$(EXTENSION) +export PGOPTIONS = -c pg_session_jwt.jwk={"crv":"Ed25519","kty":"OKP","x":"R_Abz-63zJ00l-IraL5fQhwkhGVZCSooQFV5ntC3C7M"} + +PG_CONFIG ?= pg_config +PGXS := $(shell $(PG_CONFIG) --pgxs) +include $(PGXS) \ No newline at end of file diff --git a/docker-compose/ext-src/pg_session_jwt-src/expected/basic_functions.out b/docker-compose/ext-src/pg_session_jwt-src/expected/basic_functions.out new file mode 100644 index 0000000000..ca54864ecd --- /dev/null +++ b/docker-compose/ext-src/pg_session_jwt-src/expected/basic_functions.out @@ -0,0 +1,35 @@ +-- Basic functionality tests for pg_session_jwt +-- Test auth.init() function +SELECT auth.init(); + init +------ + +(1 row) + +-- Test an invalid JWT +SELECT auth.jwt_session_init('INVALID-JWT'); +ERROR: invalid JWT encoding +-- Test creating a session with an expired JWT +SELECT auth.jwt_session_init('eyJhbGciOiJFZERTQSJ9.eyJleHAiOjE3NDI1NjQ0MzIsImlhdCI6MTc0MjU2NDI1MiwianRpIjo0MjQyNDIsInN1YiI6InVzZXIxMjMifQ.A6FwKuaSduHB9O7Gz37g0uoD_U9qVS0JNtT7YABGVgB7HUD1AMFc9DeyhNntWBqncg8k5brv-hrNTuUh5JYMAw'); +ERROR: Token used after it has expired +-- Test creating a session with a valid JWT +SELECT auth.jwt_session_init('eyJhbGciOiJFZERTQSJ9.eyJleHAiOjQ4OTYxNjQyNTIsImlhdCI6MTc0MjU2NDI1MiwianRpIjo0MzQzNDMsInN1YiI6InVzZXIxMjMifQ.2TXVgjb6JSUq6_adlvp-m_SdOxZSyGS30RS9TLB0xu2N83dMSs2NybwE1NMU8Fb0tcAZR_ET7M2rSxbTrphfCg'); + jwt_session_init +------------------ + +(1 row) + +-- Test auth.session() function +SELECT auth.session(); + session +------------------------------------------------------------------------- + {"exp": 4896164252, "iat": 1742564252, "jti": 434343, "sub": "user123"} +(1 row) + +-- Test auth.user_id() function +SELECT auth.user_id() AS user_id; + user_id +--------- + user123 +(1 row) + diff --git a/docker-compose/ext-src/pg_session_jwt-src/sql/basic_functions.sql b/docker-compose/ext-src/pg_session_jwt-src/sql/basic_functions.sql new file mode 100644 index 0000000000..6c1ab90c0c --- /dev/null +++ b/docker-compose/ext-src/pg_session_jwt-src/sql/basic_functions.sql @@ -0,0 +1,19 @@ +-- Basic functionality tests for pg_session_jwt + +-- Test auth.init() function +SELECT auth.init(); + +-- Test an invalid JWT +SELECT auth.jwt_session_init('INVALID-JWT'); + +-- Test creating a session with an expired JWT +SELECT auth.jwt_session_init('eyJhbGciOiJFZERTQSJ9.eyJleHAiOjE3NDI1NjQ0MzIsImlhdCI6MTc0MjU2NDI1MiwianRpIjo0MjQyNDIsInN1YiI6InVzZXIxMjMifQ.A6FwKuaSduHB9O7Gz37g0uoD_U9qVS0JNtT7YABGVgB7HUD1AMFc9DeyhNntWBqncg8k5brv-hrNTuUh5JYMAw'); + +-- Test creating a session with a valid JWT +SELECT auth.jwt_session_init('eyJhbGciOiJFZERTQSJ9.eyJleHAiOjQ4OTYxNjQyNTIsImlhdCI6MTc0MjU2NDI1MiwianRpIjo0MzQzNDMsInN1YiI6InVzZXIxMjMifQ.2TXVgjb6JSUq6_adlvp-m_SdOxZSyGS30RS9TLB0xu2N83dMSs2NybwE1NMU8Fb0tcAZR_ET7M2rSxbTrphfCg'); + +-- Test auth.session() function +SELECT auth.session(); + +-- Test auth.user_id() function +SELECT auth.user_id() AS user_id; \ No newline at end of file