Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add valid to schema #17

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add valid to schema #17

wants to merge 3 commits into from

Conversation

ksy_schema.json Outdated Show resolved Hide resolved
Co-authored-by: Petr Pučil <petr.pucil@seznam.cz>
ksy_schema.json Outdated
@@ -352,6 +352,10 @@
}
]
},
"valid": {
"description": "expected value that the parser should encounter at this point (or throw an error). Unlike `contents`, this is the value *after* parsing.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expected value that the parser should encounter at this point (or throw an error).

This entire sentence is weird and inaccurate (I don't like "expected value", "this point" or "(or throw an error)"). Instead of explaining how, I'll rather come up with something myself when I have time.

Unlike contents, this is the value after parsing.

What do you mean by this? Since 0.9 which introduced valid, contents has become a syntactic sugar for valid, so their runtime semantics is the same (the difference is that contents only works with fixed byte arrays and it doesn't use the expression language but a very special schema, but on the other hand the byte length is derived automatically, whereas valid also works with various data types and you can use the expression language to dynamically calculate the values used in valid/{eq,expr,any-of,min,max} constraints).

In other words, these two attributes are exactly equivalent and they generate the same code:

seq:
  - id: magic
    contents: [0xcd, 0x80]
seq:
  - id: magic
    size: 2
    valid: '[0xcd, 0x80]'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire sentence is weird and inaccurate (I don't like "expected value", "this point" or "(or throw an error)"). Instead of explaining how, I'll rather come up with something myself when I have time.

Agreed. I don't understand this very well so I just took a quick stab with my limited understanding. You're right - valid is considerably more flexible, and I'll update to fix it.

Unlike contents, this is the value after parsing.

What do you mean by this?

For instance, I have a value that's expected to be 0x0000ffff. { type: u4, valid: 0x0000ffff } checks that value after parsing (perhaps a better word would be "casting") it to its declared type (i.e. `u4').


I think the biggest limit to my understanding with the schema is that it's not quite clear which strings are supposed to be expressions, identifiers, or literal strings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants