Revert "pidl: Use non-existent function dissect_ndr_int64()"
[Samba.git] / python / samba / netcmd / validators.py
blob5690341df5b069e8caff9e2a5c809c5208c18c41
1 # Unix SMB/CIFS implementation.
3 # validators
5 # Copyright (C) Catalyst.Net Ltd. 2023
7 # Written by Rob van der Linde <rob@catalyst.net.nz>
9 # This program is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
14 # This program is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with this program. If not, see <http://www.gnu.org/licenses/>.
23 from samba.getopt import Validator, ValidationError
26 class Range(Validator):
27 """Checks if the value is within range min ... max."""
29 def __init__(self, min=None, max=None):
30 if min is None and max is None:
31 raise ValueError("Range without a min and max doesn't make sense.")
33 self.min = min
34 self.max = max
36 def __call__(self, field, value):
37 """Check if value is within the range min ... max.
39 It is possible to omit min, or omit max, in which case a more
40 tailored error message is returned.
41 """
42 if self.min is not None and self.max is None:
43 if value < self.min:
44 raise ValidationError(f"{field} must be at least {self.min}")
46 elif self.min is None and self.max is not None:
47 if value > self.max:
48 raise ValidationError(
49 f"{field} cannot be greater than {self.max}")
51 elif self.min is not None and self.max is not None:
52 if value < self.min or value > self.max:
53 raise ValidationError(
54 f"{field} must be between {self.min} and {self.max}")
57 class OneOf(Validator):
58 """Checks if the value is in a list of possible choices."""
60 def __init__(self, choices):
61 self.choices = sorted(choices)
63 def __call__(self, field, value):
64 if value not in self.choices:
65 allowed_choices = ", ".join(self.choices)
66 raise ValidationError(f"{field} must be one of: {allowed_choices}")