]> gitweb.factorcode.org Git - factor.git/blob - basis/validators/validators.factor
b9587161fd74c1f6a0b0f044175a8b333b888464
[factor.git] / basis / validators / validators.factor
1 ! Copyright (C) 2006, 2010 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs classes continuations hashtables kernel
4 make math math.functions math.parser math.ranges namespaces
5 quotations regexp sequences sets unicode words xmode.catalog ;
6 IN: validators
7
8 : v-checkbox ( str -- ? )
9     >lower "on" = ;
10
11 : v-default ( str def -- str/def )
12     [ drop empty? not ] most ;
13
14 : v-required ( str -- str )
15     dup empty? [ "required" throw ] when ;
16
17 : v-optional ( str quot -- result )
18     over empty? [ 2drop f ] [ call ] if ; inline
19
20 : v-min-length ( str n -- str )
21     over length over < [
22         [ "must be at least " % # " characters" % ] "" make
23         throw
24     ] [
25         drop
26     ] if ;
27
28 : v-max-length ( str n -- str )
29     over length over > [
30         [ "must be no more than " % # " characters" % ] "" make
31         throw
32     ] [
33         drop
34     ] if ;
35
36 : v-number ( str -- n )
37     dup string>number [ ] [ "must be a number" throw ] ?if ;
38
39 : v-integer ( str -- n )
40     v-number dup integer? [ "must be an integer" throw ] unless ;
41
42 : v-min-value ( x n -- x )
43     2dup < [
44         [ "must be at least " % # ] "" make throw
45     ] [
46         drop
47     ] if ;
48
49 : v-max-value ( x n -- x )
50     2dup > [
51         [ "must be no more than " % # ] "" make throw
52     ] [
53         drop
54     ] if ;
55
56 : v-regexp ( str what regexp -- str )
57     3dup nip matches?
58     [ 2drop ] [ drop "invalid " prepend throw ] if ;
59
60 : v-email ( str -- str )
61     ! From http://www.regular-expressions.info/email.html
62     320 v-max-length
63     "e-mail"
64     R/ [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i
65     v-regexp ;
66
67 : v-url ( str -- str )
68     "URL" R/ (?:ftp|http|https):\/\/\S+/ v-regexp ;
69
70 : v-captcha ( str -- str )
71     dup empty? [ "must remain blank" throw ] unless ;
72
73 : v-one-line ( str -- str )
74     v-required
75     dup "\r\n" intersects?
76     [ "must be a single line" throw ] when ;
77
78 : v-one-word ( str -- str )
79     v-required
80     dup [ alpha? ] all?
81     [ "must be a single word" throw ] unless ;
82
83 : v-username ( str -- str )
84     2 v-min-length 16 v-max-length v-one-word ;
85
86 : v-password ( str -- str )
87     6 v-min-length 40 v-max-length v-one-line ;
88
89 : v-mode ( str -- str )
90     dup mode-names member? [
91         "not a valid syntax mode" throw
92     ] unless ;
93
94 : luhn? ( str -- ? )
95     string>digits <reversed>
96     [ odd? [ 2 * 10 /mod + ] when ] map-index
97     sum 10 divisor? ;
98
99 : v-credit-card ( str -- n )
100     "- " without
101     dup CHAR: 0 CHAR: 9 [a..b] diff empty? [
102         13 v-min-length
103         16 v-max-length
104         dup luhn? [ string>number ] [
105             "card number check failed" throw
106         ] if
107     ] [
108         "invalid credit card number format" throw
109     ] if ;