summaryrefslogtreecommitdiffstats
path: root/acf/model/field.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf/model/field.lua')
-rw-r--r--acf/model/field.lua61
1 files changed, 61 insertions, 0 deletions
diff --git a/acf/model/field.lua b/acf/model/field.lua
index ad26818..8927dfd 100644
--- a/acf/model/field.lua
+++ b/acf/model/field.lua
@@ -106,6 +106,67 @@ function Field:validate_saved(context)
end
+local Primitive = class(Field)
+
+function Primitive:validate(context, value)
+ local t = self.dtype
+ if type(value) ~= t then raise(context.path, 'Not a '..t) end
+end
+
+
+String = class(Primitive)
+
+function String:init(params)
+ super(self, String):init(params)
+ self.dtype = 'string'
+end
+
+function String:validate(context, value)
+ super(self, String):validate(context, value)
+ if self['max-length'] and string.len(value) > self['max-length'] then
+ raise(context.path, 'Maximum length exceeded')
+ end
+end
+
+function String:meta(context)
+ local res = super(self, String):meta(context)
+ res['max-length'] = self['max-length']
+ return res
+end
+
+
+Number = class(Primitive)
+
+function Number:init(params)
+ super(self, Number):init(params)
+ self.dtype = 'number'
+end
+
+function Number:_validate(context, value)
+ return super(self, Number):_validate(
+ context,
+ value and tonumber(value) or value
+ )
+end
+
+
+Integer = class(Number)
+
+function Integer:validate(context, value)
+ super(self, Integer):validate(context, value)
+ if math.floor(value) ~= value then raise(context.path, 'Not an integer') end
+end
+
+
+Boolean = class(Primitive)
+
+function Boolean:init(params)
+ super(self, Boolean):init(params)
+ self.dtype = 'boolean'
+ self.widget = self.dtype
+end
+
+
TreeNode = class(Field)
function TreeNode:load(context, create)