AllenNeuralDynamics / aind-metadata-validator

Package to support validation checks for aind-data-schema for export to other services
MIT License
0 stars 0 forks source link

Sub-field evaluation, needs to handle all possible sub-field types (list, union, optional, etc) #3

Closed dbirman closed 1 week ago

dbirman commented 1 week ago

See _metadata_valid_helper in metadata_helpers.py in aind-metadata-viz

            if origin_type is list:
                item_type = expected_type.__args__[0]
                return all([item_type(**item_json) for item_json in json])
            elif origin_type is Optional:
                # skip optional fields!
                return True
            elif origin_type is Union:
                # Get all possible types in the Union
                union_types = get_args(expected_type)

                for union_type in union_types:
                    try:
                        return union_type(**json)
                    except ValidationError:
                        continue
                else:
                    return False
            else:
                # validate as a pydantic model
                return expected_type(**json) is not None