1.2.0 • Published 5 years ago

prop-types-from-mongoose v1.2.0

Weekly downloads
1
License
GPL-3.0
Repository
github
Last release
5 years ago

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
1.2.0

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago