--[[ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] module(..., package.seeall) local err = require('acf.error') local raise = err.raise local fld = require('acf.model.field') local String = fld.String local to_field = require('acf.model.model').to_field local object = require('acf.object') local class = object.class local super = object.super require 'stringy' Range = class(String) function Range:init(params) super(self, Range):init(params) if not self.type then self.type = fld.Integer end end function 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 Union = class(String) function Union:validate(context, value) super(self, 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