51 lines
979 B
PL/PgSQL
51 lines
979 B
PL/PgSQL
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 || ''
|
|
);
|
|
END IF;
|
|
|
|
IF _max IS NOT NULL AND _length > _max THEN
|
|
PERFORM _api.raise(
|
|
_msg => 'api_max_value',
|
|
_detail => _column,
|
|
_hint => _max || ''
|
|
);
|
|
END IF;
|
|
|
|
RETURN TRUE;
|
|
END
|
|
$BODY$;
|
|
|
|
GRANT EXECUTE ON FUNCTION _api.validate_text(TEXT, TEXT, INTEGER, INTEGER, BOOLEAN)
|
|
TO rest_anon, rest_user;
|