/**
 * File:	modules/TypeRepository.ycp
 * Package:	yast2
 * Summary:	Type repository for validation of user-defined types
 * Authors:	Stanislav Visnovsky <visnov@suse.cz>
 *
 * $Id: TypeRepository.ycp 20677 2005-01-07 03:23:55Z nashif $
 */

{

module "TypeRepository";

import "Address";
import "Hostname";
import "IP";
import "Netmask";
import "URL";

/* Map of known types, empty initially */
map<string, boolean(any)> types = $[];

/**
 * Validate, that the given value is of given type.
 *
 * @param value	value to be validated
 * @param type type against which to validate
 * @return true, if the value can be considered to be of a given type
 */
global define boolean is_a(any value, string type) ``{
    boolean(any) validator = types[type]:nil;
    if ( validator != nil )	{
	return validator(value);
    } else {
	y2error( "Request to validate unknown type %1", type  );
    }
    return false;
}

/**
 * Check, if s is a string.
 *
 * @param s		a value to be validated
 * @return		true if s is string
 */
define boolean is_string( any s ) ``{
    return is( s, string );
}

/**
 * Constructor, defines the known types.
 */
global define void TypeRepository() ``{
    types = $[
	"ip"		: IP::Check,
	"ip4"		: IP::Check4,
	"ip6"		: IP::Check6,
	"netmask"	: Netmask::Check,
	"netmask4"	: Netmask::Check4,
	"netmask6"	: Netmask::Check6,
	"host"		: Address::Check,
	"host4"		: Address::Check4,
	"host6"		: Address::Check6,
	"hostname"	: Hostname::Check,
	"fullhostname"	: Hostname::CheckFQ,
	"domain"	: Hostname::CheckDomain,
	"url"		: URL::Check,
	"string"	: is_string,
    ];
}

/************************* generic validators *******************************/

/**
 *  Generic regular expression validator.
 *
 *  @param regex	the regular expression to be matched
 *  @param value	the value to be matched
 *  @return	true if successful
 */
global define boolean regex_validator( string regex, string value ) ``{
    return regexpmatch( value, regex );
}

/**
 *  Generic enumerated type validator.
 *
 *  @param values	a list of possible values
 *  @param value	the value to be matched
 *  @return	true if successful
 */
global define boolean enum_validator( list values, string value ) ``{
    return contains( values, value );
}

/* EOF */
}
