I’m trying to refactor some old code I have lying around, and I’m trying to change all my methods to ‘ValidatedMethods’ but I’m having some issues with what I suppose is the validation, this are the files I have:
The defined schema:
import SimpleSchema from 'simpl-schema';
import 'uniforms-bridge-simple-schema-2';
import ImageField from '../../ui/components/ImageField';
const BrandsSchema = new SimpleSchema({
name: {
type: String,
},
active: {
type: Boolean,
defaultValue: true,
},
logo: {
type: Object,
uniforms: ImageField,
},
'logo.url': String,
'logo.public_id': String,
slug: {
type: String,
autoValue() {
const name = this.field('name').value;
console.log('Acá andamos...');
console.log(this.field);
return name
.toString()
.toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
},
},
});
export default BrandsSchema;
The validating method:
export const addBrand = new ValidatedMethod({
name: 'brands.insert',
validate: BrandsSchema.validator(),
run({ newBrand }) {
if (!this.userId) {
throw new Meteor.Error('unauthorized-user');
}
if (!this.userId && !Roles.userIsInRole(this.userId, 'admin')) {
throw new Meteor.Error('unauthorized-user-permission-group');
}
return Brands.insert(newBrand);
},
});
I’m sending the data using a Vzco Autoform like this:
const addBrandFromForm = (newBrand) => {
addBrand.call(
{
newBrand,
},
(err) => {
if (err) {
console.log(err);
} else {
// success!
}
},
);
};
const AdminBrandsNew = () => (
<div>
<h1 className="page-header">Agregar Marca</h1>
<AutoForm
ref={ref => (this.formRef = ref)}
schema={BrandsSchema}
onSubmit={doc => addBrandFromForm(doc)}
/>
</div>
);
But whenever I try to insert anything I get this error:
ClientError: newBrand is not allowed by the schema
Any ideas about what could be causing this error?
Thanks in advance.