Commit c270f1f 0 parents commit c270f1f Copy full SHA for c270f1f
File tree 4 files changed +108
-0
lines changed
4 files changed +108
-0
lines changed Original file line number Diff line number Diff line change
1
+ yarn.lock
2
+ .vscode
Original file line number Diff line number Diff line change
1
+ # ow-prop-type
2
+
3
+ React prop-types validation with [ ow] ( https://github.com/sindresorhus/ow )
4
+
5
+ 1 . when ` process.env.NODE_ENV ` is ` 'production' ` it will be a shim function
6
+ 2 . it exports the ` ow ` object (or shim in ` 'production' ` ) as a property
7
+ 3 . will return the unprocessed ` ArgumentError `
8
+
9
+ ## Why
10
+
11
+ [ prop-types] ( https://github.com/facebook/prop-types ) has only very basic validation, and it is super verbose to add custom validators.
12
+
13
+ ## Example
14
+
15
+ ``` js
16
+ import propType , { ow } from ' ow-prop-type'
17
+
18
+ class MyComponent extends React .Component {
19
+ static propTypes = {
20
+ // propType with a predicate
21
+ total: propType (
22
+ ow
23
+ .number
24
+ .integer
25
+ .greaterThanOrEqual (0 )
26
+ ),
27
+ // propType with a callback, must return a predicate
28
+ current: propType ((props ) => {
29
+ return ow
30
+ .number
31
+ .integer
32
+ .greaterThanOrEqual (0 )
33
+ .lessThanOrEqual (props .total )
34
+ }
35
+ }
36
+ }
37
+ ` ` `
Original file line number Diff line number Diff line change
1
+ 'use strict'
2
+
3
+ let ow , propType
4
+
5
+ function devPropType ( predicateOrCallback ) {
6
+ return typeof predicateOrCallback === 'function'
7
+ ? ( props , propName , ...args ) => {
8
+ const predicate = predicateOrCallback ( props , propName , ...args )
9
+ const value = props [ propName ]
10
+ try {
11
+ ow ( value , predicate )
12
+ } catch ( err ) {
13
+ return err
14
+ }
15
+ }
16
+ : ( props , propName ) => {
17
+ const value = props [ propName ]
18
+ try {
19
+ ow ( value , predicateOrCallback )
20
+ } catch ( err ) {
21
+ return err
22
+ }
23
+ }
24
+ }
25
+
26
+ function productionPropType ( ) { }
27
+
28
+ // copy+paste ow/dev-only.js because it does not seem to be published
29
+ if ( process . env . NODE_ENV === 'production' ) {
30
+ const shim = new Proxy ( ( ) => { } , {
31
+ get : ( ) => shim ,
32
+ apply : ( ) => shim
33
+ } )
34
+
35
+ ow = shim
36
+ propType = productionPropType
37
+ } else {
38
+ ow = require ( 'ow' )
39
+ propType = devPropType
40
+ }
41
+
42
+ // exporting ow since it might be the shim
43
+ propType . ow = ow
44
+ propType . propType = propType
45
+
46
+ module . exports = propType
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " ow-prop-type" ,
3
+ "version" : " 0.0.0" ,
4
+ "description" : " react prop-types validation with ow" ,
5
+ "keywords" : [
6
+ " ow" ,
7
+ " prop-types" ,
8
+ " react" ,
9
+ " validation"
10
+ ],
11
+ "homepage" : " https://github.com/kamicane/ow-prop-type#readme" ,
12
+ "bugs" : " https://github.com/kamicane/ow-prop-type/issues" ,
13
+ "repository" : " github:kamicane/ow-prop-type" ,
14
+ "license" : " MIT" ,
15
+ "author" : " Valerio Proietti <kamicane@gmail.com>" ,
16
+ "main" : " index.js" ,
17
+ "dependencies" : {
18
+ "ow" : " ^0.15.0"
19
+ },
20
+ "devDependencies" : {
21
+ "standard" : " ^14.3.1"
22
+ }
23
+ }
You can’t perform that action at this time.
0 commit comments