--[[ $Id: $ $DateTime: $ $Author: $ $Change: $ ]]-- --[[ This is a Lua script for validating the size of a form field. The field size is counted as number of bytes, so for some UTF characters, one character might be longer than one byte. It allows instance configuration to choose a form, form-field and a minimum-size for the field. If the field value is shorter that the expected size, the extension will fail the command with a message to the user. ]]-- package.path = Helix.Core.Server.GetArchDirFileName( "?.lua" ) local utils = require "ExtUtils" local initDone = false local validForms = { branch = 1, client = 1, label = 1, protect = 1, triggers = 1, change = 1 } function init() if not initdone then initDone = 1 utils.init() end end function GlobalConfigFields() return { "" } end function InstanceConfigFields() --formType = "the spec to validate field size in - e.g. branch/label/protect", --formField = "the field in the form to validate", --minSize = "minimum size for the field value to accept" return { formType = "The form type to check: branch/client/label/protect/triggers/change", formField = "The field to check, e.g. Description/Type/Status", minSize = "minimum field size to accept" } end function InstanceConfigEvents() init() local form = utils.iCfgData[ "formType" ] if form == nil or validForms[ form ] == nil then return false end return { ["form-in"] = form } end function FormIn() init() local form = utils.iCfgData[ "formType" ] if form == nil or validForms[ form ] == nil then local msg = "form = '".. form .."'\n" if form == nil then error( "nil form" ) end if validForms[ form ] == nil then msg = msg .. "validForms[ form ] == nil\n" error( "Invalid form selected:" .. msg ) end end local field = utils.iCfgData[ "formField" ] local minsz = utils.iCfgData[ "minSize" ] local file = Helix.Core.Server.GetVar( "formfile" ); local f = io.open( file, "r" ) if f == nil then error( "Upexpected error, form file does not exist:" .. file .. "\n" ) end local contents = f:read( "*all" ) local p4 = P4.P4:new() local parsed = p4:parse_spec( form, contents ) local val = parsed[ field ] val = trim( val ) if val:len() < tonumber( minsz ) then Helix.Core.Server.SetClientMsg( "Field '" .. field .. "' minimum size is " .. minsz .. ", '" .. val .. "' is too short." ) return false end return true end