summaryrefslogtreecommitdiff
path: root/src/db/rest/util/_api_validate_text.sql
diff options
context:
space:
mode:
authorFreya Murphy <freya@freyacat.org>2024-04-01 11:09:25 -0400
committerFreya Murphy <freya@freyacat.org>2024-04-01 11:09:25 -0400
commit3a82baec9d793edf81ac2b151b0f4d4159641375 (patch)
treef9d50c296b078ac48c2a2391c172c3ccf37edb3f /src/db/rest/util/_api_validate_text.sql
parentrefactor asset dir, refactor oberver in lib (diff)
downloadxssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.gz
xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.tar.bz2
xssbook2-3a82baec9d793edf81ac2b151b0f4d4159641375.zip
login and register, liking on homepage
Diffstat (limited to 'src/db/rest/util/_api_validate_text.sql')
-rw-r--r--src/db/rest/util/_api_validate_text.sql53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/db/rest/util/_api_validate_text.sql b/src/db/rest/util/_api_validate_text.sql
new file mode 100644
index 0000000..ff3a227
--- /dev/null
+++ b/src/db/rest/util/_api_validate_text.sql
@@ -0,0 +1,53 @@
+CREATE FUNCTION _api.validate_text(
+ _column TEXT DEFAULT '',
+ _text TEXT DEFAULT NULL,
+ _min INTEGER DEFAULT NULL,
+ _max INTEGER DEFAULT NULL,
+ _nullable BOOLEAN DEFAULT FALSE
+)
+RETURNS BOOLEAN
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+ _length INTEGER;
+BEGIN
+
+ -- make sure that text can only be null
+ -- when we allow it
+ IF _text IS NULL AND NOT _nullable THEN
+ PERFORM _api.raise(
+ _msg => 'api_null_value',
+ _detail => _column
+ );
+ END IF;
+
+ IF _text IS NULL THEN
+ RETURN TRUE;
+ END IF;
+
+ _length = LENGTH(_text);
+
+ IF _min IS NOT NULL AND _length < _min THEN
+ PERFORM _api.raise(
+ _msg => 'api_min_value',
+ _detail => _column,
+ _hint => _min || ''
+ );
+ RETURN FALSE;
+ END IF;
+
+ IF _max IS NOT NULL AND _length > _max THEN
+ PERFORM _api.raise(
+ _msg => 'api_max_value',
+ _detail => _column,
+ _hint => _max || ''
+ );
+ RETURN FALSE;
+ END IF;
+
+ RETURN TRUE;
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION _api.validate_text(TEXT, TEXT, INTEGER, INTEGER, BOOLEAN)
+ TO rest_anon, rest_user;