Child pages
  • Apply validation

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

To create a Schema, you can invoke the SaveSchema API or, more simply, log in to the Apstrata workbench and click "Manage App > Schemas > New".

...

  • The value of the score is a numeric,
  • The level is also a numeric,
  • The lives field is a numeric,
  • The player's name is a string.

So let's go ahead and write a schema. We first start with the definition of the fields. Open the workbench as recommend in the preceding paragraph, then click "Manage App > Schemas >  New". The workbench opens an editor with a predefined schema template. Head to the "<fields>" section (we will get back later to the "<aclGroups>" section).

First step is to define our fields, "score", "fields" and "lives" of type "numeric", and "player" of type "string".

Code Block
languagexml
titleschema excerpt
...
<fields>
	<field name="score" type="numeric"/>
	<field name="level" type="numeric"/>
	<field name="lives" type="numeric"/>
	<field name="player" type="string"/>
</fields>
...

...

Let us move back to the <aclGroups> section. We will not dive into it for now, just note that it is used to restrict the read/write access to your documents. For now, we will restrict read and write access on our document to the creator of that document (creator is a predefined role in Apstrata, automatically set to the user). So this This is how our final schema looks like:

...

Since it does not make sense to save a game without a score, a level or a player name for example, how can we enforce the setting of values to these fields? Well, this is simple: we just need to specify the minimum cardinality of the corresponding field, as describe described in the below example.

Code Block
languagexml
titleschema excerpt
...
<fields>
	<field name="score" type="numeric">
		<validation>
				<cardinality min="1" max="1"/>
		</validation>
	</field>
	<field name="level" type="numeric">
		<validation>
				<cardinality min="1" max="1"/>
		</validation>
	</field>
	<field name="lives" type="numeric">
		<validation>
				<cardinality min="1" max="1"/>
		</validation>
	</field>
	<field name="player" type="string">
		<validation>
				<cardinality min="1" max="1"/>
		</validation>
	</field>
</fields>
... 

...

  • The value of the score is a numeric such as score >= 0
  • The level is also a numeric such as  1 <= level <= 30 (there are 30 levels in your game)
  • The lives field is a numeric such as 0 <= lives <= 10 
  • The player's name only contains alphanumeric characters.

Here as well, we simply resort to the <validation> element:

...

You can directly try your schema from the Apstrata workbench, using the API Explorer. Log in to the workbench, then click on API Explorer then select SaveDocument.

...