module.exports = class WithValidator
	@detectBy: ['with']
	
	constructor: (@params) ->
		
	validate: (value, valid, model) ->
		valid yes
		validator = undefined
		if @params.with instanceof RegExp
			validator = RegExpValidator
		else if 'string' is typeof @params.with
			validator = CustomValidator
		else if @params.with instanceof Object
			validator = @params.with
		
		return valid(no, 'no known validator') if not validator
		
		(new validator(@params)).validate value, valid, model

class RegExpValidator
	constructor: (@params) ->
	
	validate: (value, valid) ->
		valid @params.with.test(value), @params.message or 'does not follow required format'

class CustomValidator
	constructor: (@params) ->
	
	validate: (value, valid, model) ->
		validator = model[@params.with]
		validator.call model, value, valid