xssbook2/db/rest/util/_api_validate_text.sql

52 lines
977 B
MySQL
Raw Normal View History

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_text_min',
_detail => _column,
_hint => _min || ''
);
END IF;
IF _max IS NOT NULL AND _length > _max THEN
PERFORM _api.raise(
_msg => 'api_text_max',
_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;