1

As you can see below there is an array parameter passed to the validated method. For validation I'm using SimpleSchema.

client

const url = "/articles/bmphCpyHZLhTc74Zp"
example.call({ item: url.split('/') })

server

example = new ValidatedMethod({
    name    : 'example',
    validate: new SimpleSchema({
        item: {
            type: [String]
        }
    }).validator(),

    run({ item }) {
        console.log(item)
    }
})

But I would like to validate a bit more specific. So the item array must have three elements. The first is empty, the second should use a value set by allowedValues and the third is an ID SimpleSchema.RegEx.Id

user3142695
  • 15,844
  • 47
  • 176
  • 332
  • Are you sure you want to store this as an array rather than an object? An array really should be used when each element of the array is conceptually the same thing, where each element can be interchanged with each other. The way you describe your data, conceptually, the three elements of your array are actually 3 different fields. Would it make more sense to create an object on the client from the url.split() array, and then store that in the collection? That would also make validation a lot easier. – Hashcut Jul 06 '17 at 07:51
  • @Hashcut Thanks for that. Your are totally right. – user3142695 Jul 06 '17 at 08:02

1 Answers1

0

you can implemented is using custom schema like this. pass regex as well.

AddressSchema = new SimpleSchema({
  street: {
    type: String,
    max: 100
  },
  city: {
    type: String,
    max: 50
  },
  state: {
    type: String,
    regEx: /^A[LKSZRAEP]|C[AOT]|D[EC]|F[LM]|G[AU]|HI|I[ADLN]|K[SY]|LA|M[ADEHINOPST]|N[CDEHJMVY]|O[HKR]|P[ARW]|RI|S[CD]|T[NX]|UT|V[AIT]|W[AIVY]$/
  },
  zip: {
    type: String,
    regEx: /^[0-9]{5}$/
  }
});

CustomerSchema = new SimpleSchema({
  billingAddress: {
    type: AddressSchema
  },
  shippingAddresses: {
    type: [AddressSchema],
    minCount: 1
  }
});

you can put it like this.

example = new ValidatedMethod({
    name    : 'example',
    validate: new SimpleSchema({
        item: {
            type: [CustomType],
            regEx: // your regex
        }
    }).validator(),

    run({ item }) {
        console.log(item)
    }
})
MehulJoshi
  • 879
  • 8
  • 19
  • please add what you want to validate? just like AddressSchema you can create your custom type. ok? or tell me what is your structure and i will write that for you – MehulJoshi Jul 06 '17 at 06:10