prop-types-from-mongoose
v1.2.0
Published
Generate PropTypes validation from Mongoose schema.
Downloads
10
Maintainers
Readme
prop-types-from-mongoose
Generate a validation function for PropTypes from Mongoose schema. Supports all Mongoose types and most validators.
API
Exports a single function that takes a Mongoose Schema and returns a PropType validation function.
fromSchema(schema[, refs])
Parameters
schema
Themongoose.Schema
to convert to a PropType functionrefs
Optional
Object mapping nestedSchema
s referenced fromObjectId
s to their respective names.
Return value
A PropTypes validation function.
Supported validation types
- String
- match
- maxlength
- minlength
- enum
- Number / Decimal128
- max
- min
- Boolean
- Object
- Map
- Array
- ObjectId
- ref
- Sub-documents a.k.a. embed schemas
Example
Here is an example of a React Component using PropType validation based on a Mongoose Schema:
import React, { Component } from 'react'
import { Schema } from 'mongoose'
import fromSchema from 'prop-types-from-mongoose'
let userSchema = new Schema({
name: String,
email: {
type: String
match: /^.+?@.+?$/
}
})
class Example extends Component {
render () {
return <h1>Hello { this.props.user.name }</h1>
}
}
Example.propTypes = {
user: fromSchema(userSchema)
}
export default Example