Skip to content

[Feature] Global Defaults #258

Open
Open
@josepsolerpla

Description

@josepsolerpla

There is no way to create a custom validator for all types, like "max: 3" on "number" type.

We can create default custom for individual types but is not very efficient.

defaults: { 
	"object": {
		custom(value, errors, schema) { doSomething() }
		return value;
	},
	"object": {
		custom(value, errors, schema) { doSomething() }
		return value;
	}
}

Adding a "globalDefault" on the config and adding on the 'lib > validator.js > class Validator > getRuleFromSchema > when "const rule"' is created adding the global default one.

const rule = {
	messages: Object.assign({}, this.messages, schema.messages),
	schema: deepExtend(schema, this.defaults[schema.type], this.globalDefault, { skipIfExist: true }),
	ruleFunction: ruleFunction,
};

Even doing something like an expression will be great.

defaults: { 
		"*": {
			custom(value, errors, schema) { doSomething() }
			return value;
		}
	}
}

In my case i would like to add a auth rule on moleculer params, something like this.

id: { type: "string", auth: true }

And this will be translated on rule string to:

if (schema.auth === true) {
	src.push(`
		if (isValidateToken(token) === false) {
			${this.makeError({ type: "authError", expected: "Token must to be valid", actual: "origValue", messages })}
		}
	`);
}

Moleculer.js problem: i manage to do this using middlewares but param validation is validated before middleware execute

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions