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

Array validation does not work #93

Open
Jozsan opened this issue Apr 30, 2019 · 5 comments
Open

Array validation does not work #93

Jozsan opened this issue Apr 30, 2019 · 5 comments

Comments

@Jozsan
Copy link

Jozsan commented Apr 30, 2019

Hello, i'm trying to use validate but i'm encountering a problem with Array type validation.

const Schema = require('validate')

const userSchema = new Schema({
  firstname: {
    type: 'string', // String does not work (throw error firstname must be String)
    required: true
  },
  lastname: {
    type: 'string', // String does not work (throw error lastname must be String)
    required: true
  },
  age: {
    type: 'number', // Number does not work (throw error age must be Number)
    required: true
  },
  sex: {
    type: 'string', // String does not work (throw error sex must be String)
    required: true,
    length: {min: 1, max: 1}

  },
  studies: {
    required: true,
    type: 'array', // Array and 'array' does not work (throw error studies must be of type Array)
    each: {
      label: {
        type: 'string',
        required: true
      }
    }
  }
})

let user = {
  firstname: "foo",
  lastname: 'bar',
  age: 22,
  sex: 'M',
  studies: [{
    label: 'StudyEx'
  }]
}
console.log(typeof(user.studies)) // => object
console.log(Array.isArray(user.studies)) // => true

let errors = userSchema.validate(user)

errors.forEach(error => {
  console.log(error.message) // => studies must be of type Array.
})

I don't understand why i get studies must be of type Array.
Further i don't understand why when i use type: String || Number i get an error`.

@Jozsan
Copy link
Author

Jozsan commented Apr 30, 2019

I've used : https://repl.it/languages/nodejs
Node version : v10.15.2 linux/amd64

@guillempuche
Copy link

guillempuche commented Sep 10, 2019

Me too. The same error, but in my local machine.

@eivindfjeldstad
Copy link
Owner

I'm not able to replicate this. I suspect it has something to do with repl.it

@vitorandrade-lett
Copy link

I had this error on my machine, but it was fixed when I changed the type from 'Array' to 'array'.

@kareem-abdul
Copy link

This might help
#105 (comment)

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

5 participants