{"_id":"@cody-greene/prop-types","_rev":"3-90458c20c9e75017a2a65e018e9fbff8","name":"@cody-greene/prop-types","description":"The lovable React.PropTypes interface, modified for general use","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"author":{"name":"Cody Greene","email":"cody@dudewoah.com"},"description":"The lovable React.PropTypes interface, modified for general use","keywords":["prop-types","react","validation"],"license":"MIT","name":"@cody-greene/prop-types","repository":{"type":"git","url":"git+https://github.com/cody-greene/prop-types.git"},"version":"1.0.0","eslintConfig":{"extends":"./node_modules/eslint-config/strict.yml","env":{"node":true}},"devDependencies":{"eslint-config":"github:cody-greene/eslint-config#v2.2.0"},"bugs":{"url":"https://github.com/cody-greene/prop-types/issues"},"homepage":"https://github.com/cody-greene/prop-types#readme","_id":"@cody-greene/prop-types@1.0.0","scripts":{},"_shasum":"954d6d46c04a41e34ed6e7054d03a906542f2c2d","_from":".","_npmVersion":"3.8.0","_nodeVersion":"4.4.0","_npmUser":{"name":"cody-greene","email":"cody@dudewoah.com"},"maintainers":[{"name":"cody-greene","email":"cody@dudewoah.com"}],"dist":{"shasum":"954d6d46c04a41e34ed6e7054d03a906542f2c2d","tarball":"https://registry.npmjs.org/@cody-greene/prop-types/-/prop-types-1.0.0.tgz","integrity":"sha512-kYT/gQZNPa+3GCm/ZVdzqJm5GCCTzdzPwcfxQEXvtrTH4H1OGjFKhntcvYaSQNxNGx/6PLCsF1DN76B7YysZXQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQCdrPUXKS6adebLh9q7H/8cgZGFBWrshKDoIMMAkwplZwIhAMHX6IhlQcj5T06XCdDiR9Oj7qo9+SiipA35LgPR2E3L"}]},"_npmOperationalInternal":{"host":"packages-12-west.internal.npmjs.com","tmp":"tmp/prop-types-1.0.0.tgz_1460410951726_0.46636309986934066"}}},"readme":"The loveable React.PropTypes interface, modified for general use. The Error messages generated\n\n```javascript\n// node or browser\nconst PropTypes = require('prop-types')\n```\n\n#### Built-in types\nSimple: `bool`, `string`, `number`, `array`, `object`, `func`\nComplex:\n- `oneOf(['enum1', 'enum2')`: Value must strictly equal (`===`) one of these enumerated values. Adding `null` or `undefined` to the list will make the value optional.\n\n- `oneOfType([PropTypes.*, ...])`: At least one validator must succeed\n\n- `objectOf(PropTypes.*)`: Every value in the obect should be the same type\n\n- `arrayOf(PropTypes.*)`: Every element in the array should be the same type\n\n- `shape({...})`: An object taking on a particular shape. If all props are optional, then the object itself is optional. The returned validator, unlike any other builtin, only requires two arguments `validate(val, displayName)`.\n\n> `bool`, `oneOf`, `shape`: do not directly support `.isRequired`\n\n#### Example usage (basic)\n```javascript\nconst validate = PropTypes.shape({\n  foo: PropTypes.number.isRequired,\n  bar: PropTypes.arrayOf(PropTypes.string),\n  baz: PropTypes.oneOf(['green', 'red', 'blue'])\n})\nlet err = validate({\n  foo: 29,\n  bar: ['x', 'y', 7],\n  baz: 'green'\n}, 'MyDisplayName')\nconsole.log(err.toString())\n// TypeError: MyDisplayName: \".baz[2]\" is number, expected string\n```\n\n#### Custom types\nRemeber, these type definitions are all just functions so you can write your own too! Use `PropTypes.optional(fn)` to wrap your base fn with a null/undefined check and expose the original fn as `.isRequired`\n```javascript\n/**\n * @param {any} val\n * @param {string} displayName\n * @param {string} key Optional with PropTypes.shape validator\n * @param {object} props Optional with PropTypes.shape validator\n * @return {Error|null}\n * @example customChecker(val, displayName, key, props)\n * note: Checkers should have very shallow call-stacks\n *       Avoid using things like Array.forEach() here\n */\nconst myLuckyNumber = PropTypes.optional(function (val, displayName, key, props) {\n  if (val !== 7) return new TypeError(`${displayName}: ${key} is not 7`)\n})\nconst validate = PropTypes.shape({\n  foo: myLuckyNumber.isRequired,\n  bar: PropTypes.arrayOf(myLuckyNumber)\n})\n```\n\n#### Example usage (validate background worker params)\nAdd parameter validation to job handlers for something like [node-resque][] or [arquebus][] where JSON.parse() is used to generate input. This pattern could also apply to querystring parameters when writing a REST API, or untrusted input when building a dynamic SQL statement.\n```javascript\n/**\n * Define a background job similar to how React Components are defined\n * @param {string} displayName\n * @param {object} propTypes\n * @param {function} perform(props, done)\n * @return {function}\n */\nfunction createJob(opt) {\n  const validate = PropTypes.shape(opt.propTypes)\n  return function validateAndRun(props, done) {\n    let err = validate(props, opt.displayName, 'props')\n    if (err) done(err)\n    else opt.perform(props, done)\n  }\n}\n\nconst calsync = createJob({\n  displayName: 'CalendarSync',\n  propTypes: {\n    uid: PropTypes.string.isRequired,\n    fromDate: PropTypes.number.isRequired,\n    private: PropTypes.bool\n  },\n  perform(props, done) {\n    console.log(props)\n    done()\n  }\n}\n\n// Register the job handler with node-resque or arquebus\n// Or just run it directly for the sake of a short example\ncalsync({\n  uid: '010101010101',\n  private: true\n}, function (err) {\n  if (err) console.log(err.stack)\n  else console.log('done!')\n})\n// TypeError: CalendarSync \"props.fromDate\" is required\n```\n","maintainers":[{"name":"cody-greene","email":"cody@dudewoah.com"}],"time":{"modified":"2022-06-12T16:20:43.444Z","created":"2016-04-11T21:42:32.170Z","1.0.0":"2016-04-11T21:42:32.170Z"},"homepage":"https://github.com/cody-greene/prop-types#readme","keywords":["prop-types","react","validation"],"repository":{"type":"git","url":"git+https://github.com/cody-greene/prop-types.git"},"author":{"name":"Cody Greene","email":"cody@dudewoah.com"},"bugs":{"url":"https://github.com/cody-greene/prop-types/issues"},"license":"MIT","readmeFilename":"readme.md"}