Child pages
  • Apply validation
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Structured documents

Using Apstrata, you can define a structure that can be used to create your documents. This structure, written in XML, is called a "schema". Schemas allow you to define rules that are automatically applied to documents referring to them guaranteeing thus the consistency of the data they contain.

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".

Example 1: the game document schema

So you are implementing this cool game that runs on mobile devices. At some point, your users need to save their game so they can come back later and start from where they left. Let us assume that what you have to save is the player's name, the current level and score, as well as the remaining "lives" of the player. You also want to make sure that:

  • 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.

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 defined our fields, "score", "fields" and "lives" of type "numeric", and "player" of type "string"

<fields>
	<field name="score" type="numeric"/>
	<field name="level" type="numeric"/>
	<field name="lives" type="numeric"/>
	<field name="player" type="string"/>
</fields>

That's cool. Now we know that documents based on this schema will contains the aforementioned fields.

But wait... how do we make these fields mandatory (i.e. we cannot save a game without a score)? That is simple: we just need to specify the minimum cardinality of the corresponding field:

<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>
 

Now that's better! Using the <validation> and <cardinality> elements, we specified for each field the minimum and maximum occurrences (note that when max is greater than one, then multiple value occurrences are allowed for the given field, which is automatically turned into an array).

Let's move now to the last part of our requirements and add new validation rules

<fields>
	<field name="score" type="numeric">
		<validation>
				<cardinality min="1" max="1"/>
				<range min="0"/> <!-- The score cannot be less than 0 -->
		</validation>
	</field>
	<field name="level" type="numeric">
		<validation>
				<cardinality min="1" max="1"/>
				<range min="0" max="30" /> <!-- The level is a value between 0 and 30 -->
		</validation>
	</field>
	<field name="lives" type="numeric">
		<validation>
				<cardinality min="1" max="1"/>
				<range min="0" max="10" /> <!-- A player can have 0 from 10 lives -->
		</validation>
	</field>
	<field name="player" type="string">
		<validation>
				<cardinality min="1" max="1"/>
				<regex>^([a-zA-Z0-9@*#]{3,10})$</regex> <!-- The player's name is an alphanumeric string of 3 to 10 characters -->
		</validation>

	</field>
</fields>
  • No labels