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

Clean function returns different value with different order of fields. #204

Closed
flexi-xu opened this issue Nov 27, 2017 · 2 comments
Closed

Comments

@flexi-xu
Copy link

flexi-xu commented Nov 27, 2017

var SimpleSchema = require('simpl-schema').default;

var ss = new SimpleSchema({
	name: {
		type: String,
		defaultValue: 'Jackie'
	},
	message: {
		type: String,
		autoValue: function () {
			return "Hello, " + this.field('name').value;
		}
	}
});

console.log(ss.clean({}));
// result: { name: 'Jackie', message: 'Hello, Jackie' }

Then change the order of the field 'name' and 'message'

var SimpleSchema = require('simpl-schema').default;

var ss = new SimpleSchema({
	message: {
		type: String,
		autoValue: function () {
			return "Hello, " + this.field('name').value;
		}
	},
	name: {
		type: String,
		defaultValue: 'Jackie'
	}
});

console.log(ss.clean({}));
// result: { message: 'Hello, undefined', name: 'Jackie' }

The 2 results are different. I expect the message will always be 'Hello, Jackie'.

@aldeed
Copy link
Collaborator

aldeed commented Nov 27, 2017

@flexi-xu This is the way it is unfortunately. The clean function has no way of knowing that the message autoValue relies on the name value, so order is important. It does ensure that shallow nested objects and arrays run before their deeper descendants, but for fields at the same level, schema order matters. I will add this to the documentation.

@aldeed aldeed closed this as completed Nov 27, 2017
@junwu-metix
Copy link

If I don't like name has a defaultValue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants