Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def check_array_fn(v):
if not isinstance(v, (list, tuple)):
raise ValidationError(
f'Expected array but got {type(v).__name__!r}',
)
for i, val in enumerate(v):
with validate_context(f'At index {i}'):
inner_check(val)
return check_array_fn
def check_one_of_fn(v):
if v not in possible:
possible_s = ', '.join(str(x) for x in sorted(possible))
raise ValidationError(
f'Expected one of {possible_s} but got: {v!r}',
)
def check_type_tag(tag):
if tag not in ALL_TAGS:
raise cfgv.ValidationError(
'Type tag {!r} is not recognized. '
'Try upgrading identify and pre-commit?'.format(tag),
)
def check_regex(v):
try:
re.compile(v)
except re.error:
raise ValidationError(f'{v!r} is not a valid python regex')
def load_from_filename(
filename,
schema,
load_strategy,
exc_tp=ValidationError,
):
with reraise_as(exc_tp):
if not os.path.exists(filename):
raise ValidationError(f'{filename} does not exist')
with open(filename, encoding='utf-8') as f:
contents = f.read()
with validate_context(f'File {filename}'):
try:
data = load_strategy(contents)
except Exception as e:
raise ValidationError(str(e))
validate(data, schema)
return apply_defaults(data, schema)
def _require_key(self, dct):
if self.key not in dct:
raise ValidationError(f'Missing required key: {self.key}')
def check(self, dct):
if dct.get('repo') in {LOCAL, META}:
self._cond('rev').check(dct)
self._cond('sha').check(dct)
elif 'sha' in dct and 'rev' in dct:
raise cfgv.ValidationError('Cannot specify both sha and rev')
elif 'sha' in dct:
self._cond('sha').check(dct)
else:
self._cond('rev').check(dct)