<commit_before>local json = require("json")
local wrong_function_name = "WrongFunctionName"
local generic_response = "GenericResponse"

local module = {
  mt = {__index = { } }
}

function module.CreateSchemaValidator(schema)
  res = { }
  res.schema = schema
  setmetatable(res, module.mt)
  return res
end

local function dump(o)
  if type(o) ~= 'table' then
    return tostring(o)
  end
  local s = '{ '
  for k,v in pairs(o) do
    if type(k) ~= 'number' then k = '"'..k..'"' end
    s = s .. '['..k..'] = ' .. dump(v) .. ','
  end
  return s .. '} \n'  
end

local function errorMsgToString(tbl)
  local tmp = ''
  if type(tbl) ~= 'table' then
    return tostring(tbl)
  end
  for _,v in pairs(tbl) do
    if type(v) == 'table' then
      tmp = tmp ..errorMsgToString(v)
    else
      if v~="" then
       tmp = tmp ..v ..'\n'
      end
    end
  end
  return tmp  
end

-- Check that all mandatory parameters in current schema are existing
local function CheckExistenceOfMandatoryParam(func_schema, user_data, name_of_structure)
  local error_message = {}
  local result = true

  for key,value in pairs(func_schema) do
      if (type(user_data)~='table')then
        return false, "not valid type of "..key .. " expected structure, got "..type(user_data)
      end
      local bool_result = true
      if((user_data[key]==nil) and func_schema[key]["mandatory"] == true) then
        bool_result = false
        if (name_of_structure ~= nil) then 
          key = name_of_structure.."."..key
        end
        error_message["mandatory "..key]= "mandatory parameter "..key.." not present"
      end
      result = bool_result and result
    end
  return result, error_message
end

-- Get names of function or structures
local function GetNames( name )
  if (string.find(name, "%.")) then
    return name:match("([^.]+).([^.]+)")
  end
  return 'Ford Sync RAPI', name
end

-- Compare structs. For each structure should be called 
-- check of mandatory params and types for all elements in the struct
function module.mt.__index:CompareStructs( data_elem, struct_schema,name_of_structure)
  local mandatory_check_result = true --for mandatory param
  local type_parameter_check_result = true -- for correct types of param
  local error_message = {}
  local error_message2 = {}

  local struct_params = struct_schema["param"]
  mandatory_check_result, error_message= CheckExistenceOfMandatoryParam(struct_params, data_elem, name_of_structure)
  type_parameter_check_result, error_message2 = self:CheckTypeOfParam(struct_params, data_elem, error_message, name_of_structure)

  local result = (mandatory_check_result and type_parameter_check_result)
  if (error_message2~=nil) then
    for k,v in pairs(error_message2) do error_message[k] = v end
  end
  return result, error_message
end

-- If data is empty, then we think it is empty array
-- Json's isArray recieve false in this case
-- If data is non empty then we check data using json
local function isArray(data)
  if next(data) == nil then return true,0 end 
  return json.isArray(data);
end

-- Calls if element has attribute array=true
-- Check that element is array
-- For each element of array call CompareType with isArray=false
function module.mt.__index:CheckTypesInArray( data_elem, schemaElem, nameofParameter, name_of_structure)
  local result = true
  local error_message  = {}
  local elem1 = type(data_elem)
  if isArray(data_elem) then
    for key, value in pairs(data_elem) do
      result, error_message[key]= self:CompareType(value, schemaElem, false, nameofParameter.."."..key, name_of_structure)
    end
  else 
    if name_of_structure~=nil then 
      return false, "Parameter "..name_of_structure.."."..nameofParameter..": got "..elem1..", expected Array"
    end
    return false, "Parameter "..nameofParameter..": got "..elem1..", expected Array"
  end
  return result, error_message
end


-- Check that length of value more than minlength and less then maxlength
local function CheckLength( data_elem, schemaElem, data_name )
  local length = string.len(data_elem)<commit_msg>Fix validation of array Before we check that data is empty, it should be checked that it is table<commit_after>local json = require("json")
local wrong_function_name = "WrongFunctionName"
local generic_response = "GenericResponse"

local module = {
  mt = {__index = { } }
}

function module.CreateSchemaValidator(schema)
  res = { }
  res.schema = schema
  setmetatable(res, module.mt)
  return res
end

local function dump(o)
  if type(o) ~= 'table' then
    return tostring(o)
  end
  local s = '{ '
  for k,v in pairs(o) do
    if type(k) ~= 'number' then k = '"'..k..'"' end
    s = s .. '['..k..'] = ' .. dump(v) .. ','
  end
  return s .. '} \n'  
end

local function errorMsgToString(tbl)
  local tmp = ''
  if type(tbl) ~= 'table' then
    return tostring(tbl)
  end
  for _,v in pairs(tbl) do
    if type(v) == 'table' then
      tmp = tmp ..errorMsgToString(v)
    else
      if v~="" then
       tmp = tmp ..v ..'\n'
      end
    end
  end
  return tmp  
end

-- Check that all mandatory parameters in current schema are existing
local function CheckExistenceOfMandatoryParam(func_schema, user_data, name_of_structure)
  local error_message = {}
  local result = true

  for key,value in pairs(func_schema) do
      if (type(user_data)~='table')then
        return false, "not valid type of "..key .. " expected structure, got "..type(user_data)
      end
      local bool_result = true
      if((user_data[key]==nil) and func_schema[key]["mandatory"] == true) then
        bool_result = false
        if (name_of_structure ~= nil) then 
          key = name_of_structure.."."..key
        end
        error_message["mandatory "..key]= "mandatory parameter "..key.." not present"
      end
      result = bool_result and result
    end
  return result, error_message
end

-- Get names of function or structures
local function GetNames( name )
  if (string.find(name, "%.")) then
    return name:match("([^.]+).([^.]+)")
  end
  return 'Ford Sync RAPI', name
end

-- Compare structs. For each structure should be called 
-- check of mandatory params and types for all elements in the struct
function module.mt.__index:CompareStructs( data_elem, struct_schema,name_of_structure)
  local mandatory_check_result = true --for mandatory param
  local type_parameter_check_result = true -- for correct types of param
  local error_message = {}
  local error_message2 = {}

  local struct_params = struct_schema["param"]
  mandatory_check_result, error_message= CheckExistenceOfMandatoryParam(struct_params, data_elem, name_of_structure)
  type_parameter_check_result, error_message2 = self:CheckTypeOfParam(struct_params, data_elem, error_message, name_of_structure)

  local result = (mandatory_check_result and type_parameter_check_result)
  if (error_message2~=nil) then
    for k,v in pairs(error_message2) do error_message[k] = v end
  end
  return result, error_message
end

-- If data is empty, then we think it is empty array
-- Json's isArray recieve false in this case
-- If data is non empty then we check data using json
local function isArray(data)
  if(type(data)~='table') then return false end
  if next(data) == nil then return true end 
  return json.isArray(data);
end

-- Calls if element has attribute array=true
-- Check that element is array
-- For each element of array call CompareType with isArray=false
function module.mt.__index:CheckTypesInArray( data_elem, schemaElem, nameofParameter, name_of_structure)
  local result = true
  local error_message  = {}
  local elem1 = type(data_elem)
  if isArray(data_elem) then
    for key, value in pairs(data_elem) do
      result, error_message[key]= self:CompareType(value, schemaElem, false, nameofParameter.."."..key, name_of_structure)
    end
  else 
    if name_of_structure~=nil then 
      return false, "Parameter "..name_of_structure.."."..nameofParameter..": got "..elem1..", expected Array"
    end
    return false, "Parameter "..nameofParameter..": got "..elem1..", expected Array"
  end
  return result, error_message
end


-- Check that length of value more than minlength and less then maxlength
local function CheckLength( data_elem, schemaElem, data_name )
  local length = string.len(data_elem)
{'fraction_non_alphanumeric': 0.10567135239941833, 'fraction_numerical': 0.001817741153659719, 'mean_word_length': 3.5799112097669257, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '493806', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2523, 'n_words': 834}