Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type format validators and cardinality validators should be injected #927

Open
SimonCockx opened this issue Feb 18, 2025 · 0 comments
Open
Labels
enhancement New feature or request subject: code generation This issue is about code generation

Comments

@SimonCockx
Copy link
Contributor

SimonCockx commented Feb 18, 2025

Right now, the type format validator and the cardinality validator of a class are instantiated directly by the corresponding Meta class of a pojo. This results in less configurable generated code (e.g., consumers cannot override the behaviour of some validators) and makes it hard to implement future enhancements which may require dependency injection such as supporting conditions on type aliases. (see #879)

I would propose to inject them in the same way as we currently inject conditions, i.e., using a ValidatorFactory argument.

Below is an example from the CDM.

Current meta code for type BondReference:

@RosettaMeta(model=BondReference.class)
public class BondReferenceMeta implements RosettaMetaData<BondReference> {

	@Override
	public List<Validator<? super BondReference>> dataRules(ValidatorFactory factory) {
		return Arrays.asList(
			factory.<cdm.product.asset.BondReference>create(cdm.product.asset.validation.datarule.BondReferenceBondUnderlier.class)
		);
	}
	
	...

	@Override
	public Validator<? super BondReference> validator() {
		return new BondReferenceValidator();
	}

	@Override
	public Validator<? super BondReference> typeFormatValidator() {
		return new BondReferenceTypeFormatValidator();
	}

        ...
}

This would change to:

@RosettaMeta(model=BondReference.class)
public class BondReferenceMeta implements RosettaMetaData<BondReference> {

	@Override
	public List<Validator<? super BondReference>> dataRules(ValidatorFactory factory) {
		return Arrays.asList(
			factory.<BondReference>create(BondReferenceBondUnderlier.class)
		);
	}
	
	...

	@Override
	public Validator<? super BondReference> validator(ValidatorFactory factory) {
		return factory.<BondReference>create(BondReferenceValidator.class);
	}

	@Override
	public Validator<? super BondReference> typeFormatValidator(ValidatorFactory factory) {
		return factory.<BondReference>create(BondReferenceTypeFormatValidator.class);
	}

        // For backwards compatibility
	@Deprecated
	@Override
	public Validator<? super BondReference> validator() {
		return new BondReferenceValidator();
	}

	@Deprecated
	@Override
	public Validator<? super BondReference> typeFormatValidator() {
		return new BondReferenceTypeFormatValidator();
	}

        ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request subject: code generation This issue is about code generation
Projects
None yet
Development

No branches or pull requests

1 participant