longshotlabs/simpl-schema

Clean function returns different value with different order of fields.

flexi-xu opened this issue · 2 comments

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'.

@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.

If I don't like name has a defaultValue?