You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here, offset is dependent on lib_file: It is required if lib_file is provided, but it should not be provided if lib_file is not provided - an "exclusive nor" relationship. Thus, offset should be validated conditionally, rather than being simply required or optional.
I have figured out a quite hacky way to do this, namely:
lib_file="C:\lib.so"offset="0xFFF"schema=Schema({ Optional("lib_file"): Use(open),
"offset": Or(And(lambdaoffset: offsetisnotNoneorlib_fileisNone,
error="If you provide a libfile, you must also provide an offset"),
Use(lambdaoffset: int(offset, 16)))
}, ignore_extra_keys=True)
schema.validate({"lib_file": lib_file, "offset": offset})
# Not okay - offset but no liblib_file="C:\lib.so"offset=Noneschema.validate({"lib_file": lib_file, "offset": offset})
schema.SchemaError: If you provide a libfile, you must also provide an offset
However, this way is not at all ideal. First off, I need to access the store lib_file value in a variable and access it inside the schema, since I'm accessing it inside offset's validation logic. Second, this is extremely ugly and bulky. It would be great if there was some way to create relations (mutually exclusive, jointly exhaustive, exclusive nor) between keys, and make a key optional or required depending on if another key exists or not:
Required_if("other_key", "this_key")
Optional_if("other_key", "this_key")
# Perhaps more complex logic could be implemented around a type of design pattern, allowing for more complicated conditions (just like with the validation itself)
Or maybe a dictionary of relationships passed to the Schema constructor:
I have a value that I only want to validate if a different argument is provided. Here is a simplified version of my code:
Here,
offset
is dependent onlib_file
: It is required iflib_file
is provided, but it should not be provided iflib_file
is not provided - an "exclusive nor" relationship. Thus,offset
should be validated conditionally, rather than being simply required or optional.I have figured out a quite hacky way to do this, namely:
This yields the desired effects:
However, this way is not at all ideal. First off, I need to access the store
lib_file
value in a variable and access it inside the schema, since I'm accessing it insideoffset
's validation logic. Second, this is extremely ugly and bulky. It would be great if there was some way to create relations (mutually exclusive, jointly exhaustive, exclusive nor) between keys, and make a key optional or required depending on if another key exists or not:Or maybe a dictionary of relationships passed to the
Schema
constructor:This would make my code as simple as:
Or maybe there's already a way to do what I want that I'm not aware of?
Thanks.
The text was updated successfully, but these errors were encountered: