Changes between Version 2 and Version 3 of Doc/panc/tips
- Timestamp:
- Sep 4, 2007, 3:40:29 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Doc/panc/tips
v2 v3 1 1 = Record Validation = 2 2 3 Often one wants to ensure that several entries in a record are consistent. For example, here one wants to ensure that the number of children is consistent with the given maximum. To remember where to put the validation code, keep in mind that you're validating the record and *not* the entries of the record. Thus, the 'with' clause should be tied to the record definition. 3 4 {{{ 4 type structure_enclosure = { 5 'type' : string with match(self, 'blade|dumb|hypervisor') 6 'children' : string[1..] with is_profile_list(self) 5 type my_kids = { 6 'children' : string[1..] with is_profile_list(SELF) 7 7 'maxchildren' ? long(1..) 8 8 } with { 9 if (exists( self['maxchildren'])) {10 self['maxchildren'] >= length(self['children']);9 if (exists(SELF['maxchildren'])) { 10 SELF['maxchildren'] >= length(SELF['children']); 11 11 } else { 12 12 true; … … 14 14 }; 15 15 }}} 16 17 For the older compiler (v6) you must define an intermediate type as a 'with' clause on a record definition is not valid in the older compiler. 18 19 That is, 20 16 For the older compiler (v6) does not allow a 'with' clause to be put on a record definition. For that compiler, you must define an intermediate type. 21 17 {{{ 22 type structure_enclosure = { 23 'type' : string with match(self, 'blade|dumb|hypervisor') 18 type my_kids = { 24 19 'children' : string[1..] with is_profile_list(self) 25 20 'maxchildren' ? long(1..) 26 } with {27 if (exists(self['maxchildren'])) {28 self['maxchildren'] >= length(self['children']);29 } else {30 true;31 };32 21 }; 33 22 34 type validated_structure_enclosure=35 structure_enclosurewith {23 type my_validated_kids = 24 my_kids with { 36 25 if (exists(self['maxchildren'])) { 37 26 self['maxchildren'] >= length(self['children']); … … 41 30 }; 42 31 }}} 43 32 This same technique can be used with any resource (list, nlist, record).