Getting started with CionΒΆ
Create a venv however you prefer, and then install cion from pip.
pip install cion
Then you can use it anywhere in your code.
import cion
LoginSchema = cion.Schema(
fields={
"username": cion.Field(
filters=[
cion.types.string(),
cion.validators.length(3, 64)
],
required=True
),
"password": cion.Field(
filters=[
cion.types.string(),
cion.validators.length(3, 1024)
],
required=True
)
}
)
LoginSchema.validate({
"username": "meizuflux",
"password": "password"
})
First, we can create a Schema
import cion
LoginSchema = cion.Schema()
Next, we add fields.
LoginSchema = cion.Schema(
fields={
"username": cion.Field(),
"password": cion.Field()
}
)
After we have our fields, we can define filters. Filters are just functions that take the value and do something with it.
For instance, cion.validators.length() validates the length of the value.
LoginSchema = cion.Schema(
fields={
"username": cion.Field(
filters=[
cion.types.string(),
cion.validators.length(3, 64)
],
required=True
),
"password": cion.Field(
filters=[
cion.types.string(),
cion.validators.length(3, 1024)
],
required=True
)
}
)
Finally, after we have defined our schema, we can validate some data.
LoginSchema.validate({
"username": "meizuflux",
"password": "password"
})
# this works, as the values go through all of the filters without raising an error
LoginSchema.validate({
"username": "m",
"password": "password"
})
# this raises an error, as the username must be at least 3 characters in length
Read on in the rest of the documentation to learn how :exception:`cion.ValidationError` works, how filters work, and how to build and extend onto cion.
Full code.
import cion
LoginSchema = cion.Schema(
fields={
"username": cion.Field(
filters=[
cion.types.string(),
cion.validators.length(3, 64)
],
required=True
),
"password": cion.Field(
filters=[
cion.types.string(),
cion.validators.length(3, 1024)
],
required=True
)
}
)
LoginSchema.validate({
"username": "meizuflux",
"password": "password"
})