Skip to content

Major Updates Ahoy

Compare
Choose a tag to compare
@grofit grofit released this 14 Jun 14:20
· 57 commits to master since this release

This new version contains a few updates, some for everyone, some just for typescript users.

For Typescript people

So you can now use almost all validation group methods as async, which allows you to do the following:

let modelErrors = await validationGroup.getModelErrors();
console.log(modelErrors);

To support this we have added a dependency on tslib for the runtime component for this, which stops the crazy async/await output in every file.

For everyone else

Well we have added support for 2 new features, the first being composite rules which allow you to create validation rules that apply to the whole model, so in these instances you will be passed the model resolver and it is up to you how you validate the model. You can create pre-made ICompositeValidationRule objects or dynamic rules at builder time which will automatically create a bespoke implementation behind the scenes. Information about this can be found in the docs for ruleset building, but here is a simple example:

let ruleSet = createRuleset()
	.forProperty("number1").addRule("minValue", 1)
	.addDynamicRule("totalNumber",
		x => {
			 let total = x.resolve("number1") + x.resolve("number2");
			 return Promise.resolve(total > 10);
		},
		"The total is not greater than 10") // <- THIS CREATES A COMPOSITE RULE
	.build();

let model = {
	number1: 0,
	number2: 0
};

let valGroup = createGroup().build(model, ruleSet);

let initialErrors = await valGroup.getModelErrors(true);
expect(initialErrors).to.include.keys("number1");
expect(initialErrors).to.include.keys("totalNumber"); // <- VIRTUAL PROPERTY NAMES ARE OUTPUT IN ERRORS

The other addition is display names so you can now add display name hints to properties via the builder or ruleset which are exposed by validation groups. Internally this isn't really used by treacherous core, but it will be used within the view related libraries to allow you to output human readable display names for property errors in validation summaries rather than the name of the actual property, like so:

let displayName = "User's name";
let rulesetBuilder = new RulesetBuilder();
let ruleset = rulesetBuilder.create()
	.forProperty("username")
		.addRule("required")
		.withDisplayName(displayName) // <-- SET THE DISPLAY NAME
	.build();

let dummyModel = {
	username: ""
};

let validationGroup = createValidationGroupFor(dummyModel, ruleset);
let actualDisplayName = validationGroup.getPropertyDisplayName("username");  // <-- GET THE DISPLAY NAME

expect(actualDisplayName).to.equal(displayName);