summaryrefslogtreecommitdiffstats
path: root/aconf/model/combination.lua
blob: cc145c3fabfc8d9f94d1dd272c712872dd3593ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
--[[
Copyright (c) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details
--]]

local M = {}

local err = require('aconf.error')
local raise = err.raise

local fld = require('aconf.model.field')
local String = fld.String

local to_field = require('aconf.model.model').to_field

local object = require('aconf.object')
local class = object.class
local super = object.super


local stringy = require('stringy')


M.Range = class(String)

function M.Range:init(params)
   super(self, M.Range):init(params)
   if not self.type then self.type = fld.Integer end
end

function M.Range:validate(context, value)
   local comps = stringy.split(value, '-')
   if #comps > 2 then raise(context.path, 'Invalid range') end
   for _, v in ipairs(comps) do to_field(self.type):_validate(context, v) end
end


M.Union = class(String)

function M.Union:validate(context, value)
   super(self, M.Union):validate(context, value)
   for _, tpe in ipairs(self.types) do
      local field = to_field(tpe)
      if err.call(field.validate, field, context, value) then return end
   end
   raise(context.path, self.error or 'Invalid value')
end


return M