prop-types-from-mongoose

    1.2.0 • Public • Published

    prop-types-from-mongoose

    Build Status Coverage Status

    NPM

    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
      The mongoose.Schema to convert to a PropType function

    • refs Optional
      Object mapping nested Schemas referenced from ObjectIds 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
    

    Install

    npm i prop-types-from-mongoose

    DownloadsWeekly Downloads

    2

    Version

    1.2.0

    License

    GPL-3.0

    Unpacked Size

    14.7 kB

    Total Files

    8

    Last publish

    Collaborators

    • redhatter