Comments (7)
Hi.
It would make a little bit easier if you could provide an example data structure that you want to validate. It should be rather easy to do something with partial-schemas
and the include
directives. Also it would help some with what you really want to validate in the data structure. Are you after validating all file names is valid or directory names?
from pykwalify.
I'm currently working on a tool to interpret and check statecharts (see sismic). I use YAML to define statecharts. In a nutshell, a statechart is a set of states, and each state can also have nested states and so one.
Here is a simplified example:
statechart:
name: Elevator
initial: active
# Skipped part
# ...
states:
- name: active
parallel states:
- name: movingElevator
initial: doorsOpen
states:
- name: doorsOpen
transitions:
- target: doorsClosed
guard: destination != current
action: doors.close()
- target: doorsClosed
guard: after(10) and current > 0
action: |
destination = 0
doors.close()
- name: doorsClosed
transitions:
- target: movingUp
guard: destination > current
- target: movingDown
guard: destination < current and destination >= 0
- name: moving
transitions:
- target: doorsOpen
guard: destination == current
action: doors.open()
states:
- name: movingUp
on entry: current = current + 1
transitions:
- target: movingUp
guard: destination > current
- name: movingDown
on entry: current = current - 1
transitions:
- target: movingDown
guard: destination < current
- name: floorListener
initial: floorSelecting
states:
- name: floorSelecting
transitions:
- target: floorSelecting
event: floorSelected
action: destination = event.floor
The root element, namely statechart
defines a states
sequence. Each item in this sequence is a state (defined by a name and that can have many other attributes). A state can be basic (meaning it has no nested state), composite (meaning it has nested states, defined using states:
) or parallel (meaning it has nested parallel states, defined using parallel states:
).
I would like to validate this kind of YAML definition of a statechart. "Classical attributes" are easy to define, but I don't see how to say "a state can have nested states, which in turn can have nested states, which in turn can have nested states, which...". I need some kind of recursive definition:
[type state]
- name: str
- (optional) type: [final | shallow memory | deep memory]
- (optional) transitions: sequence of [type transition]
- (optional) states: sequence of [type state]
- (optional) parallel states: sequence of [type state]
- (optional) on entry: str
- (optional) on exit: str
[type transition]
- (optional) event: str
- (optional) guard: str
- (optional) action: str
- (optional) target: str (ideally, a foreign key to a [type state].name)
Thank you!
from pykwalify.
Oh yeah, that is supported right now. You should be looking at the following feature https://github.com/Grokzen/pykwalify/blob/unstable/docs/Validation%20Rules.md#partial-schemas
Basically you define the top level statechard
level and then you implement a partial schema that defined the following data level
- name: foo
initial: bar
states:
type: seq
include: foobar
Just to get you started on a schema that you can finish up is the following
schema;foo:
type: map
mapping:
name:
type: str
states:
type: seq
sequence:
- type: map
include: foo
initial:
type: str
parallel-states:
type: map
include: foo
type: map
mapping:
statechart:
type: map
include: foo
This schema validates for me with your test data you posted (with one exception that i changed to paralell-states
but that is just semantics)
I leave it up to you to finish the rest of the schema :]
One thing i can agree on is that the docs could be lacking in describing that nested schema is supported with the schema;
and include
tag. I might make some new examples and update the docs to be abit more clear on this feature in the future.
If you have other questions or want to chat some more, i suggest joining in the gitter chatroom and ping me in there and we can continue in there.
from pykwalify.
I'll have a look at partial-schemas.
Thanks for your answer!
I'll probably get back with new questions ;-)
from pykwalify.
I still have to do some modifications, but here's the resulting schema. Thank you again!
schema;contract:
type: seq
sequence:
- type: map
mapping:
"before":
type: str
- type: map
mapping:
"after":
type: str
- type: map
mapping:
"always":
type: str
schema;transition:
type: map
mapping:
"target":
type: str
"event":
type: str
"guard":
type: str
"action":
type: str
"contract":
include: contract
schema;state:
type: map
mapping:
"name":
type: str
required: yes
"initial":
type: str
"on entry":
type: str
"on exit":
type: str
"type":
type: str
enum: [final, shallow history, deep history]
"states":
type: seq
sequence:
- include: state
"parallel states":
type: seq
sequence:
- include: state
"transitions":
type: seq
sequence:
- include: transition
"contract":
include: contract
type: map
mapping:
"statechart":
type: map
required: yes
mapping:
"name":
type: str
required: yes
"description":
type: str
"on entry":
type: str
"initial":
type: str
required: yes
"states":
type: seq
required: yes
sequence:
- include: state
"contract":
include: contract
from pykwalify.
Awesome that you got it to work :]
I hope you do not mind if i use a subset of the final answer in a example file for Advance Recursive Schema Example
from pykwalify.
Yeah, feel free to use it ;)
from pykwalify.
Related Issues (20)
- Deprecate imp module HOT 1
- type: int has a limit in python 2.7 HOT 1
- Union of schemas HOT 4
- rule.py is missing a comma in defined_keywords list
- Deprecation warning due to invalid escape sequences in Python 3.8 HOT 1
- Fails to regex match a mapping with integer keys HOT 1
- AttributeError: 'int' object has no attribute 'startswith'
- Support multiple data type HOT 1
- New release? Maintainers needed or? HOT 2
- Deprecation warning "the imp module is deprecated in favour of importlib" HOT 1
- pattern rule not working with all scalar types HOT 1
- <RuleError: error code 4
- Using fulmatch for regex matching. HOT 1
- Schema Validation Against Multiple Files HOT 1
- Outputting Scan Results in SARIF Format HOT 1
- How can I check key only? HOT 5
- anchors to inherit properties are falsely reported as Duplicate HOT 2
- ReadTheDocs does not seem to be updating
- Some error message for length violations are wrong
- issue while using "unique: true" for the sequence data type HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pykwalify.