Aurelia-validation plugin in NodeJs

This is a follow on question to an issue I was working through before when I was just trying to get my code to run in Node. BindingEngine in a nodejs application - #15 by elitemike

I have my project running, but I have not gotten Aurelia-Validation to work yet because when my code tries to create the validationRules, the plugin errors out with Did you forget to add ".plugin('aurelia-validation')" to your main.js? Since this is a node project and I am not starting up aurelia, how do I make the plugin work in this fashion? Any help would be greatly appreciated as getting this code to run in both the browser and on the server is high priority.

This is how I am setting up validation for the my stuff. Basically ValidationRules.ensure(myProperty).someRule().someRule().on(myObject) is the syntax I am using

            ValidationRules.ensure((c: StringControlConfiguration) => c.value)
            .required()
            .when(() => control.validation.required && control.canValidate)
            .withMessage(`${control.label || control.placeholder} is required`)
            .satisfies((value: string) => {
                return value !== null ? value.indexOf(control.promptChar) === -1 : true;
            })
            .when(() => control.mask !== null && control.promptChar !== null && control.canValidate)
            .withMessage(`${control.label || control.placeholder} does not meet the format: ${control.mask}`)
            .matches(control.validation.regEx)
            .when(() => control.validation.regEx !== null && control.canValidate)
            .withMessage(`${control.label || control.placeholder} is invalid (${control.validation.regEx})`)
            .satisfies(control.isValidRegEx)
            .when(() => control.inputType === 'regularExpression' && control.canValidate)
            .withMessage(`${control.label || control.placeholder} is not a valid regular expression`)
            .maxLength(control.validation.maxLength)
            .when(() => control.validation.maxLength !== null && control.canValidate)
            .withMessage(
                `${control.label || control.placeholder} must be less than ${
                    control.validation.maxLength
                } characters`
            )
            .minLength(control.validation.minLength)
            .when(() => control.validation.minLength !== null && control.canValidate)
            .withMessage(
                `${control.label || control.placeholder} must be greater than ${
                    control.validation.minLength
                } characters`
            ).satisfies((value: string) => {
                return value !==null ? !control.validation.restrictedValues.some((x) => x.toLowerCase() === value.toLowerCase()): true
            })
            .when(() => control.validation.restrictedValues !== null)
            .withMessage(`${control.label || control.placeholder || "Input"} cannot contain the current value`)
            .on(control);
1 Like

I may have solved the problem but I need to further test to make sure nothing weird is happening. If I pass the ValidationRules class to my existing code and set that as a property instead of relying on it being static, things seem to be working.

1 Like