Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 39537
1
###############################################################################
2
#
3
# CoCalc: Collaborative web-based calculation
4
# Copyright (C) 2017, Sagemath Inc.
5
# AGPLv3
6
#
7
###############################################################################
8
9
###
10
Custom Prop Validation for immutable.js types, so they work just like other
11
React prop-types.
12
13
FUTURE: Put prop validation code in a debug area so that it doesn't get loaded for production
14
15
In addition to React Prop checks, we implement the following type checkers:
16
immutable,
17
immutable.List,
18
immutable.Map,
19
immutable.Set,
20
immutable.Stack,
21
which may be chained with .isRequired just like normal React prop checks
22
23
Additional validations may be added with the following signature
24
rtypes.custom_checker_name<function (
25
props,
26
propName,
27
componentName,
28
location,
29
propFullName,
30
secret
31
) => <Error-Like-Object or null>
32
>
33
Check React lib to see if this has changed.
34
35
###
36
37
check_is_immutable = (props, propName, componentName="ANONYMOUS", location, propFullName) ->
38
if not props[propName]? or props[propName].toJS?
39
return null
40
else
41
type = typeof props[propName]
42
return new Error(
43
"Invalid prop `#{propName}` of" +
44
" type #{type} supplied to" +
45
" `#{componentName}`, expected an immutable collection or frozen object."
46
)
47
48
allow_isRequired = (validate) ->
49
check_type = (isRequired, props, propName, componentName="ANONYMOUS", location) ->
50
if not props[propName]? and isRequired
51
return new Error("Required prop `#{propName}` was not specified in `#{componentName}`")
52
return validate(props, propName, componentName, location)
53
54
chainedCheckType = check_type.bind(null, false)
55
chainedCheckType.isRequired = check_type.bind(null, true)
56
chainedCheckType.isRequired.category = "IMMUTABLE"
57
chainedCheckType.category = "IMMUTABLE"
58
59
return chainedCheckType
60
61
create_immutable_type_required_chain = (validate) ->
62
check_type = (immutable_type_name, props, propName, componentName="ANONYMOUS") ->
63
if immutable_type_name and props[propName]?
64
T = immutable_type_name
65
if not props[propName].toJS?
66
return new Error("NOT EVEN IMMUTABLE, wanted immutable.#{T} #{props}, #{propName}")
67
if require('immutable')["#{T}"]["is#{T}"](props[propName])
68
return null
69
else
70
return new Error(
71
"Component `#{componentName}`" +
72
" expected #{propName} to be an immutable.#{T}" +
73
" but was supplied #{props[propName]}"
74
)
75
else
76
return validate(props, propName, componentName, location)
77
78
# To add more immutable.js types, mimic code below.
79
check_immutable_chain = allow_isRequired check_type.bind(null, undefined)
80
check_immutable_chain.Map = allow_isRequired check_type.bind(null, "Map")
81
check_immutable_chain.List = allow_isRequired check_type.bind(null, "List")
82
check_immutable_chain.Set = allow_isRequired check_type.bind(null, "Set")
83
check_immutable_chain.Stack = allow_isRequired check_type.bind(null, "Stack")
84
check_immutable_chain.category = "IMMUTABLE"
85
86
return check_immutable_chain
87
88
exports.immutable = create_immutable_type_required_chain(check_is_immutable)
89