Closed laurentpayot closed 8 years ago
Same with astronomy@1.0.0-rc.4.
I've updated documentation to state it clearly. In the field's definition you use singular form validator:
fields: {
languages: {
/* ... */
validator: V.each()
}
}
However in the class definition you use validators because you define validators for many fields:
Item = Astro.Class({
name: 'Item',
/* ... */
validators: {
languages: V.each()
}
});
After reading the new docs it is still a bit confusing... Could you please complete the following example with all the validation lines needed to ensure languages
is an array of strings with at most 100 elements, and that every strings in this array are 2 characters long? That would really makes things clearer, thanks in advance...
Item = Astro.Class({
name: 'Item',
/* ... */
fields: {
languages: {
type: 'array',
nested: 'string'
}
}
});
Item = Astro.Class({
name: 'Item',
/* ... */
fields: {
'languages': {
type: 'array',
nested: 'string',
default: function() {
return [];
},
validator: [
// The array can have up to 100 elements
Validators.maxLength(100),
// Now every element of array has to...
Validators.every(
Validators.and([
// ... be a string and...
Validators.string(),
// ... has to be exactly 2 characters long
Validators.length(2)
])
)
]
}
}
});
I will add this example to the documentation because in deed it may be difficult at the beginning.
UPDATE: I've fixed example. There was an error. you have to pass validator to the every
validator not array of validators.
Thanks @jagi, I was getting crazy with weird errors before you updated the example above ;) I can finally validate my arrays of strings :+1:
However I need to generate custom error messages (i18n), how can we tell wich one of minLength validator triggered an error in the following example? (e.data.fieldValue
is the array in both cases)
'names': {
index: true,
type: 'array',
nested: 'string',
validator: [V.required(), V.array(), V.minLength(1), V.every(V.and([V.minLength(2), V.maxLength(201)]))]
},
Hmm right now it's not possible to tell which value caused error. I will try fixing it. Will let you know if I figured something out.
Ok, for now I'll try to find a workaround.
Ok I've managed to fix it. It will appear in the next RC version.
:+1:
With the new 1.0.0 RC 2 version of validators (didn't test with RC 1), along with astronomy 1.0.0 RC 3, I'm using the following code (
V = Validators
):With or without the validators line (it is unclear if we can use both validators and validator), I get the Meteor error
TypeError: Object #