@ -79,6 +89,22 @@ module.exports = function generateValidator(rule, name = "<unknown>") {
returnnewerrors.ValidationError(`Expected object of a type with the ${rule._name} trait, got ${getValueType(value)} instead`);
returnnewerrors.ValidationError(`Expected object of a type with the ${rule._name} trait, got ${getValueType(value)} instead`);
}
}
};
};
}elseif(rule._isRegistryTrait===true){
if(registry==null){
thrownewError("Registry-based type rules can only be used within the context of a type registry");
}else{
baseRule=function(value){
/* TODO: The below approach requires that traits be defined before the types that use them, and disallows trait registry references in `.implements` calls, due to _implementedTraits always needing to contain actual trait definitions; in the future, a better approach needs to be found for this such that trait registry references can be used everywhere. */
letactualRule=registry._traits.get(rule._name);
/* FIXME: Better error when the trait is unknown */
returnnewerrors.ValidationError(`Expected object of a type with the ${rule._name} trait, got ${getValueType(value)} instead`);
}
};
}
}elseif(rule._isSelfRule===true){
}elseif(rule._isSelfRule===true){
baseRule=function(value){
baseRule=function(value){
if(valueinstanceofthis._type){
if(valueinstanceofthis._type){
@ -150,7 +176,7 @@ module.exports = function generateValidator(rule, name = "<unknown>") {
}
}
}else{
}else{
/* FIXME: Possibly special-case (for better performance) if the only extra rule is a 'default value' rule? This would avoid a `for` loop in the case where a value is explicitly specified. */
/* FIXME: Possibly special-case (for better performance) if the only extra rule is a 'default value' rule? This would avoid a `for` loop in the case where a value is explicitly specified. */
/* FIXME: Disallow usage of registry.type-style references from one registry in types of another? Or perhaps embed the registry in the reference, instead of passing it through functions by means of scope? */