-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidators.go
787 lines (723 loc) · 22.1 KB
/
validators.go
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
package tri
import (
"time"
"reflect"
"errors"
"fmt"
"unicode"
)
// Validate checks to ensure the contents of this node type satisfy constraints.
// Brief only contains one thing, so we make sure it has it - one string. This string may not contain any type of control characters, and is limited to 80 characters in length.
func (r *Brief) Validate() error {
R := (*r)
if len(R) != 1 {
return errors.New("Brief field must have (only) one item")
}
s, ok := R[0].(string)
if !ok {
return errors.New("Brief's mandatory field is not a string")
}
if len(s) > 80 {
return errors.New("Brief's text may not be over 80 characters in length")
}
for i, x := range s {
if unicode.IsControl(x) {
return fmt.Errorf("Brief text may not contain any control characters, one found at position %d", i)
}
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// This validator only has to check the elements of the slice are zero or more Command items, and a valid name at index 0.
func (r *Command) Validate() error {
R := *r
if len(R) < 1 {
return errors.New("empty Command")
}
s, ok := R[0].(string)
if !ok {
return fmt.Errorf("first element of Command must be a string")
}
if e := ValidName(s); e != nil {
return fmt.Errorf("error in name of Command: %v", e)
}
// validSet is an array of 4 elements that represent the presence of the 4 mandatory parts.
var validSet [2]bool
brief, handler := 0, 1
var singleSet [4]bool
usage, short, help, examples := 0, 1, 2, 3
for i, x := range R[1:] {
switch c := x.(type) {
case Short:
if singleSet[short] {
return fmt.Errorf("only one Short field allowed in Command")
}
singleSet[short] = true
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case Brief:
if validSet[brief] {
return fmt.Errorf("only one Brief permitted in a Command, second found at index %d", i)
}
validSet[brief] = true
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case Usage:
if singleSet[usage] {
return fmt.Errorf("only one Usage field allowed in Command")
}
singleSet[usage] = true
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case Help:
if singleSet[help] {
return fmt.Errorf("only one Help field allowed in Command")
}
singleSet[help] = true
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case Examples:
if singleSet[examples] {
return fmt.Errorf("only one Examples field allowed in Command")
}
singleSet[examples] = true
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case Var:
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case Trigger:
e := c.Validate()
if e != nil {
return fmt.Errorf("error in Command at index %d: %v", i, e)
}
case func(*Tri) int:
if validSet[handler] {
return fmt.Errorf("only one Handler permitted in a Command, second found at index %d", i)
}
validSet[handler] = true
if c == nil {
return fmt.Errorf("nil handler in Command found at index %d", i)
}
default:
return fmt.Errorf("invalid type present in Command: %v", reflect.TypeOf(c))
}
}
if !validSet[brief] {
return errors.New("Brief field must be present")
}
if !validSet[handler] {
return errors.New("Command must have a handler")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// This validator only triggers the validator on its elements.
func (r *Commands) Validate() error {
R := (*r)
for i, x := range R {
e := x.Validate()
if e != nil {
return fmt.Errorf("error in element %d of Commands list: %v", i, e)
}
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// The only constraint on the Default subtype is that it contains at only one element, the value is checked for correct typing by the Commands validator.
func (r *Default) Validate() error {
R := (*r)
if len(R) != 1 {
return errors.New("the Default container must only contain one element")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// The constraint of DefaultCommand is that it has at least one element, and that the 0 element is a string. The check for the command name's presence in the Commands set is in the Tri validator.
func (r *DefaultCommand) Validate() error {
R := (*r)
if len(R) != 1 {
return errors.New(
"the DefaultCommand element must contain only one element")
}
s, ok := R[0].(string)
if !ok {
return errors.New("element 0 of DefaultCommand must be a string")
}
if e := ValidName(s); e != nil {
return fmt.Errorf("error in DefaultCommand: %v", e)
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// RunAfter is a simple flag that indicates by existence of an empty value, so it is an error if it has anything inside it.
func (r *DefaultOn) Validate() error {
R := *r
if len(R) > 0 {
return errors.New(
"DefaultOn may not contain anything, empty declaration only")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// The constraints of examples are minimum two elements and all elements are strings. The intent is the even numbered items are snippets showing invocation and a description string of the same format as Brief{}.
func (r *Examples) Validate() error {
R := *r
if len(R) < 2 {
return errors.New("Examples field may not be empty")
}
if len(R)%2 != 0 {
return fmt.Errorf(
"Examples must be in pairs, odd number of elements found")
}
for i, x := range R {
_, ok := x.(string)
if !ok {
return fmt.Errorf(
"Examples elements may only be strings, element %d is not a string", i)
}
}
for i := 1; i <= len(R)-1; i += 2 {
if len(R[i-1].(string)) > 40 {
return errors.New(
"Examples example text may not be over 4 characters in length")
}
if len(R[i].(string)) > 80 {
return errors.New(
"Examples explainer text may not be over 80 characters in length")
}
for i, x := range R[i].(string) {
if unicode.IsControl(x) {
return fmt.Errorf(
"Examples even numbered field string may not contain control characters, one found at index %d", i)
}
}
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// A group must contain one string, anything else is invalid. It also has the same limitation as a name - only letters.
func (r *Group) Validate() error {
R := *r
if len(R) != 1 {
return errors.New("Group must (only) contain one element")
}
s, ok := R[0].(string)
if !ok {
return errors.New("Group element must be a string")
}
if e := ValidName(s); e != nil {
return fmt.Errorf("error in name of Group: %v", e)
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Help may only contain one string. It will be parsed as markdown format and possibly can be set to style it with ANSI codes.
func (r *Help) Validate() error {
R := *r
if len(R) != 1 {
return errors.New("Help field must contain (only) one item")
}
_, ok := R[0].(string)
if !ok {
return errors.New("Help field element is not a string")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// RunAfter is a simple flag that indicates by existence of an empty value, so it is an error if it has anything inside it.
func (r *RunAfter) Validate() error {
R := *r
if len(R) > 0 {
return errors.New(
"RunAfter may not contain anything, empty declaration only")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Short names contain only a single Rune variable.
func (r *Short) Validate() error {
R := *r
if len(R) != 1 {
return errors.New("Short name item must contain (only) one item")
}
s, ok := R[0].(rune)
if !ok {
return errors.New("Short's element must be a rune (enclose in '')")
}
if !(unicode.IsDigit(s) || unicode.IsLetter(s)) {
return errors.New("Short element is not a letter or number")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Slot may only contain one type of element. The type check is in the Var, here we only ensure the slots contain pointers to the same type, the parser will put the final parsed value in all of them. Multiple variables are permitted here to enable the configuration of more than one application.
func (r *Slot) Validate() error {
R := *r
var slotTypes []reflect.Type
for _, x := range R {
slotTypes = append(slotTypes, reflect.TypeOf(x))
}
for i, x := range slotTypes {
if i > 0 {
if slotTypes[i] != slotTypes[i-1] {
return fmt.Errorf("slot contains more than one type of variable, found %v at index %d", x, i)
}
}
}
for _, x := range R {
if reflect.ValueOf(x).Kind() != reflect.Ptr {
return fmt.Errorf("slot contains non-pointer type")
}
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Terminates is a flag value, and may not contain anything.
func (r *Terminates) Validate() error {
R := *r
if len(R) > 0 {
return errors.New("Terminates type may not contain anything")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// A Tri, the base type, in a declaration must contain a name as first element, a Brief, Version and a Commands item, and only one of each. Also, this and several other subtypes of Tri.
func (r *Tri) Validate() error {
R := *r
if len(R) < 3 {
return errors.New("a Tri must contain at least 3 elements: name, Brief and Version")
}
// validSet is an array of 4 elements that represent the presence of the 4 mandatory parts.
var validSet [2]bool
brief, version := 0, 1
var singleSet [3]bool
defcom, commands := 0, 1
n, ok := R[0].(string)
if !ok {
return errors.New("first element of a Tri must be a string")
}
if e := ValidName(n); e != nil {
return fmt.Errorf("error in name of Tri: %v", e)
}
// The mandatory elements also may not be repeated:
for i, x := range R {
if i == 0 {
continue
}
switch y := x.(type) {
case Brief:
if validSet[brief] {
return fmt.Errorf(
"Tri contains more than one Brief, second found at index %d", i)
}
validSet[brief] = true
if e := y.Validate(); e != nil {
return fmt.Errorf("Tri field %d: %s", i, e)
}
case Version:
if validSet[version] {
return fmt.Errorf(
"Tri contains more than one Version, second found at index %d", i)
}
validSet[version] = true
if e := y.Validate(); e != nil {
return fmt.Errorf("Tri field %d: %s", i, e)
}
validSet[version] = true
case Commands:
if singleSet[commands] {
return fmt.Errorf(
"Tri contains more than one Commands, second found at index %d", i)
}
singleSet[commands] = true
e := y.Validate()
if e != nil {
return fmt.Errorf("error in Tri field %d: %s", i, e)
}
case Var:
e := y.Validate()
if e != nil {
return fmt.Errorf("error in Tri at index %d: %v", i, e)
}
case Trigger:
e := y.Validate()
if e != nil {
return fmt.Errorf("error in Tri at index %d: %v", i, e)
}
case DefaultCommand:
if singleSet[defcom] {
return fmt.Errorf(
"Tri contains more than one DefaultCommand, second found at index %d", i)
}
singleSet[defcom] = true
e := y.Validate()
if e != nil {
return fmt.Errorf("Tri field %d: %s", i, e)
}
commname := y[0].(string)
// DefaultCommand must match in its name one of the Command items in also present Commands array
foundComm := false
foundDefComm := false
for _, a := range R {
switch c := a.(type) {
case Commands:
foundComm = true
for _, b := range c {
if b[0].(string) == commname {
foundDefComm = true
}
}
default:
}
}
if !foundComm {
return errors.New("DefaultCommand with no Commands array present")
} else if !foundDefComm {
return errors.New("DefaultCommand found with no matching Command")
}
default:
return fmt.Errorf(
"Tri contains an element type it may not contain at index %d", i)
}
}
switch {
case !validSet[brief]:
return errors.New("Tri is missing its Brief field")
case !validSet[version]:
return errors.New("Tri is missing its Version field")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Trigger must contain (one) name, Brief and Handler, and nothing other than these and Short, Usage, Help, Default, Terminates, RunAfter.
func (r *Trigger) Validate() error {
R := *r
if len(R) < 3 {
return errors.New(
"Trigger must contain a name, Brief and Handler at minimum")
}
name, ok := R[0].(string)
if !ok {
return errors.New("first element of Trigger must be the name")
} else if e := ValidName(name); e != nil {
return fmt.Errorf("Invalid Name in Trigger at index 0: %v", e)
}
// validSet is an array that represent the presence of the mandatory parts.
var validSet [2]bool
brief, handler := 0, 1
var singleSet [7]bool
short, usage, help, defon, terminates, runafter, group := 0, 1, 2, 3, 4, 5, 6
for i, x := range R[1:] {
switch y := x.(type) {
case Brief:
if validSet[brief] {
return fmt.Errorf("Trigger may (only) contain one Brief, second found at index %d", i)
} else {
validSet[brief] = true
}
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case func(*Tri) int:
if validSet[handler] {
return fmt.Errorf(
"Trigger may (only) contain one Handler, second found at index %d", i)
} else {
validSet[handler] = true
}
if y == nil {
return fmt.Errorf("Handler at index %d may not be nil", i)
}
case Short:
if singleSet[short] {
return fmt.Errorf("Trigger may only contain one Short, extra found at index %d", i)
}
singleSet[short] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case Usage:
if singleSet[usage] {
return fmt.Errorf("Trigger may only contain one Usage, extra found at index %d", i)
}
singleSet[usage] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case Help:
if singleSet[help] {
return fmt.Errorf("Trigger may only contain one Help, extra found at index %d", i)
}
singleSet[help] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case DefaultOn:
if singleSet[defon] {
return fmt.Errorf("Trigger may only contain one DefaultOn, extra found at index %d", i)
}
singleSet[defon] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case Terminates:
if singleSet[terminates] {
return fmt.Errorf("Trigger may only contain one Terminates, extra found at index %d", i)
}
singleSet[terminates] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case RunAfter:
if singleSet[runafter] {
return fmt.Errorf("Trigger may only contain one RunAfter, extra found at index %d", i)
}
singleSet[runafter] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
case Group:
if singleSet[group] {
return fmt.Errorf(
"Trigger may only contain one Group, extra found at index %d", i)
}
singleSet[group] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Trigger contains invalid element at %d - %s", i, e)
}
default:
return fmt.Errorf(
"found invalid item type at element %d in a Trigger", i)
}
}
if !(validSet[brief] && validSet[handler]) {
return errors.New("Trigger must contain one each of Brief and Handler")
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Usage fields contain only one string of no more than 80 characters and no control characters.
func (r *Usage) Validate() error {
R := *r
if len(R) != 1 {
return errors.New("Usage field must contain (only) one element")
}
s, ok := R[0].(string)
if !ok {
return errors.New("Usage field element is not a string")
}
if ll := len(s); ll > 80 {
return fmt.Errorf("Usage string is %d chars long, may not be longer than 80", ll)
}
for i, x := range s {
if unicode.IsControl(x) {
return fmt.Errorf(
"Usage field string may not contain control characters, one found at index %d", i)
}
}
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// Var must contain name, Brief and Slot, and optionally, Short, Usage, Help and Default. The type in the Slot and the Default must be the same.
func (r *Var) Validate() error {
R := *r
if len(R) < 3 {
return errors.New(
"Var must contain a name, Brief and Slot at minimum")
}
name, ok := R[0].(string)
if !ok {
return errors.New("first element of Var must be the name")
} else if e := ValidName(name); e != nil {
return fmt.Errorf("Invalid Name in Var at index 0: %v", e)
}
// validSet is an array that represent the presence of the mandatory parts.
var validSet [2]bool
brief, slot := 0, 1
// singleSet is an array representing the optional elements that may not be more than one inside a Var
var singleSet [5]bool
short, usage, help, def, group := 0, 1, 2, 3, 4
for i, x := range R[1:] {
switch y := x.(type) {
case Brief:
if validSet[brief] {
return fmt.Errorf("Var may must (only) contain one Brief, second found at index %d", i)
} else {
validSet[brief] = true
}
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
case Short:
if singleSet[short] {
return fmt.Errorf("Var may only contain one Short, extra found at index %d", i)
}
singleSet[short] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
case Usage:
if singleSet[usage] {
return fmt.Errorf("Var may only contain one Usage, extra found at index %d", i)
}
singleSet[usage] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
case Help:
if singleSet[help] {
return fmt.Errorf("Var may only contain one Help, extra found at index %d", i)
}
singleSet[help] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
case Default:
if singleSet[def] {
return fmt.Errorf("Var may only contain one Default, extra found at index %d", i)
}
singleSet[def] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
for _, z := range R {
s, ok := z.(Slot)
if ok {
switch s[0].(type) {
case *string:
_, ok := y[0].(string)
if !ok {
return errors.New("slot is not same type as default")
}
case *int:
_, ok := y[0].(int)
if !ok {
return errors.New("slot is not same type as default")
}
case *uint32:
_, ok := y[0].(uint32)
if !ok {
return errors.New("slot is not same type as default")
}
case *float64:
_, ok := y[0].(float64)
if !ok {
return errors.New("slot is not same type as default")
}
case *[]string:
_, ok := y[0].([]string)
if !ok {
return errors.New("slot is not same type as default")
}
case *time.Duration:
_, ok := y[0].(time.Duration)
if !ok {
return errors.New("slot is not same type as default")
}
}
// *s[0] = *y[0]
}
}
case Slot:
if validSet[slot] {
return fmt.Errorf("Var may only contain one Slot, extra found at index %d", i)
}
validSet[slot] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
case Group:
if singleSet[group] {
return fmt.Errorf(
"Var may only contain one Group, extra found at index %d", i)
}
singleSet[group] = true
if e := y.Validate(); e != nil {
return fmt.Errorf(
"Var contains invalid element at %d - %s", i, e)
}
default:
return fmt.Errorf(
"found invalid item type at element %d in a Var", i)
}
}
if !(validSet[brief] && validSet[slot]) {
return errors.New("Var must contain one each of Brief and Slot")
}
// TODO: check that Default value can be assigned to dereferenced Slot variable
return nil
}
// Validate checks to ensure the contents of this node type satisfy constraints.
// A version item contains three integers and an optional (less than 16 character) string, and the numbers may not be more than 99.
func (r *Version) Validate() error {
R := *r
if len(R) > 4 {
return errors.New("Version field may not contain more than 4 fields")
}
if len(R) < 3 {
return errors.New("Version field must contain at least 3 fields")
}
for i, x := range R[:3] {
n, ok := x.(int)
if !ok {
return fmt.Errorf("Version field %d is not an integer: %d", i, n)
}
if n > 99 {
return fmt.Errorf("Version field %d value is over 99: %d", i, n)
}
}
if len(R) > 3 {
s, ok := R[3].(string)
if !ok {
return fmt.Errorf("optional field 4 of Version is not a string")
} else {
for i, x := range s {
if !(unicode.IsLetter(x) || unicode.IsDigit(x)) {
return fmt.Errorf(
"optional field 4 of Version contains other than letters and numbers at position %d: '%v,", i, x)
}
}
}
}
return nil
}
// ValidName checks that a Tri name element that should be a name only contains letters.
func ValidName(s string) error {
if len(s) < 3 {
return errors.New("name is less than 3 characters long")
}
for i, x := range s {
if !unicode.IsLetter(x) {
return fmt.Errorf(
"element %d, '%v' of name is not a letter", i, x)
}
}
return nil
}