Skip to content
This repository has been archived by the owner on Jan 18, 2024. It is now read-only.

Commit

Permalink
First pass at regex constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
ethho committed Dec 10, 2023
1 parent 02d81c5 commit e04645f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
42 changes: 37 additions & 5 deletions datajoint_file_validator/constraint/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dataclasses import dataclass
from typing import Any
from typing import Any, Iterable
from cerberus import Validator
from ..config import config
from ..snapshot import Snapshot
from ..result import ValidationResult
Expand Down Expand Up @@ -53,11 +54,32 @@ def validate(self, snapshot: Snapshot) -> ValidationResult:
class SchemaConvertibleConstraint(Constraint):

def to_schema(self) -> Schema:
"""Convert this constraint to a Cerberus schema."""
"""
Convert this constraint to a Cerberus schema that each file in
the Snapshot will be validated against.
"""
raise NotImplementedError("Subclass of SchemaConvertibleConstraint must implement to_schema() method.")

@staticmethod
def _validate_file(schema: Schema, file: dict) -> Validator:
v = Validator(allow_unknown=True)
v.validate(file, schema)
return v

def validate(self, snapshot: Snapshot) -> ValidationResult:
"""Validate a snapshot against a single constraint."""
schema: Schema = self.to_schema()
assert isinstance(schema, dict)
v = Validator(allow_unknown=True)
validators: Iterable[Validator] = list(map(lambda file: self._validate_file(schema, file), snapshot))
return ValidationResult(
status=all(validators),
message=None if all(validators) else {
file["path"]: validator.errors
for file, validator in zip(snapshot, validators)
},
context=dict(snapshot=snapshot, constraint=self)
)
breakpoint()
raise NotImplementedError()

Expand All @@ -68,9 +90,19 @@ class RegexConstraint(SchemaConvertibleConstraint):
val: str

def to_schema(self) -> Schema:
"""Convert this constraint to a Cerberus schema."""
raise NotImplementedError()
return {"regex": self.val}
"""
Convert this constraint to a Cerberus schema that each file in
the Snapshot will be validated against.
"""
return {
"path": {
"type": "string",
"required": True,
"regex": self.val
}
}




@dataclass
Expand Down
1 change: 1 addition & 0 deletions datajoint_file_validator/validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def validate_snapshot(
"""
manifest = Manifest.from_yaml(manifest_path)
results = list(map(lambda rule: rule.validate(snapshot), manifest.rules))
breakpoint()
return results


Expand Down

0 comments on commit e04645f

Please sign in to comment.