Skip to content

Validation of parsed data with assertions #435

Open
@GreyCat

Description

Extracting easier part of #81, with a concrete proposal on implementation.

Context: we want to have some sort of way to state that certain attributes have some sort of "valid" values. Stating that has two implications:

  1. All values which are "not valid" must trigger an error of parsing
  2. In some cases, we can calculate values automatically during generation of stream

The simplest existing implementation is contents: it is essentially a byte array, which validates that its contents are exactly as specified in ksy file. On parsing, we'll throw an error if byte array contents won't match the expected value. On generation, we can just write the expected value, and not bother the end-user with setting the attribute manually.

Syntax proposal

Add valid key to the attribute spec. Inside it, there could be:

  • A string, integer or boolean => it will be treated as expression that's supposed to be the only valid value of this attribute. For example:
- id: foo
  type: u4le
  valid: 0x42 # expected in stream: 42 00 00 00
- id: bar
  type: strz
  valid: '"abcd"' # expected in stream: 61 62 63 64 00
- id: baz
  type: u2le
  valid: '2 * foo' # expected in stream: 84 00
  • A map, which can feature keys:
    • eq: expected states that attribute value must be equal to given value (i.e. the same as above)
    • min: expected states minimum valid value for this attribute (compared with a >= expected)
    • max: expected states maximum valid value for this attribute (compared with a <= expected)
    • any-of: list-of-expected states that valid value must be equal to one in the given list of expressions
    • expr: expression states that given expression need to be evaluated (substituting _ with an actual attribute value) to be true to treat attribute value as valid.

All expected and expression values are KS expression strings, which can be constants or can depend on other attributes. list-of-expected must be a YAML array of KS expression strings. expected inferred type must be comparable with type of the attribute. expression inferred type must be boolean.

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions