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

[Feature] Global Defaults #258

Open
josepsolerpla opened this issue Jul 21, 2021 · 0 comments
Open

[Feature] Global Defaults #258

josepsolerpla opened this issue Jul 21, 2021 · 0 comments

Comments

@josepsolerpla
Copy link

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

@josepsolerpla josepsolerpla changed the title Global Default [Feature] Global Defaults Jul 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants