-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlib.coffee
66 lines (52 loc) · 2.21 KB
/
lib.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {check, Match} from 'meteor/check'
# From Meteor's random/random.js.
UNMISTAKABLE_CHARS = '23456789ABCDEFGHJKLMNPQRSTWXYZabcdefghijkmnopqrstuvwxyz'
INVALID_ID_CHARS_REGEX = new RegExp "[^#{ UNMISTAKABLE_CHARS }]"
INVALID_SHA256_CHARS_REGEX = new RegExp '[^a-f0-9]'
# Regular expression for matching e-mail addresses.
EMAIL_REGEX = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
Match.PositiveNumber = Match.Where (x) ->
check x, Number
x > 0
Match.NonNegativeInteger = Match.Where (x) ->
check x, Match.Integer
x >= 0
Match.NonEmptyString = Match.Where (x) ->
check x, String
x.trim().length > 0
Match.DocumentId = Match.Where (x) ->
check x, String
check x, Match.Where (y) -> y.length is 17
not INVALID_ID_CHARS_REGEX.test x
Match.ObjectWithOnlyStrings = Match.Where (x) ->
check x, Object
for key, value of x
check key, String
check value, String
Match.Enumeration = (pattern, enumeration) ->
values = _.values enumeration
Match.Where (a) ->
check a, pattern
a in values
Match.SHA256String = Match.Where (x) ->
check x, String
check x, Match.Where (y) -> y.length is 64
not INVALID_SHA256_CHARS_REGEX.test x
Match.EMail = Match.Where (x) ->
check x, Match.NonEmptyString
EMAIL_REGEX.test x
# We provide our own optional that also allows null.
Match.OptionalOrNull = (pattern) ->
# We implement it inside Match.Optional so that Match.OptionalOrNull keeps the special semantics
# inside objects where it means that object field can not exist (instanceof is used to check for
# this). Thus Match.OptionalOrNull inside objects allows that field does not exist, that it exists
# but it is null, or that it is the pattern. We additionally allow for undefined because in simulation
# on the client a value can be undefined, but when serialized and send to the server it gets transformed
# into the null. So we want the same check to work both on the client and server.
Match.Optional Match.OneOf pattern, null, undefined
Match.OrNull = (pattern) ->
Match.OneOf pattern, null
Match.RegexString = (regex) ->
Match.Where (x) ->
regex.test x
export {check, Match}