Child pages
  • Apply validation

Versions Compared

Key

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

...

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

...

Code Block
<schema>
	<aclGroups>		
		<aclGroup name="creator"> 
			<read>creator</read>
			<write>creator</write>
			<fields> <!-- the below fields are only accessible in read/write to the creator of the document -->
				<field>score</field>
				<field>level</field>
				<field>lives</field>
				<field>player</field>
			</fields>
		</aclGroup>
		<schemaAcl> <!-- nobody, except the application owner is entitled to read or modify the current schema -->
			<read>nobody</read>
			<write>nobody</write>
			<delete>nobody</delete>
		</schemaAcl>
	</aclGroups>
	<fields> <!-- Documents based on the current schema can contain the following fields -->
		<field name="score" type="numeric"/>
		<field name="level" type="numeric"/>
		<field name="lives" type="numeric"/>
		<field name="player" type="string"/>
</fields>
</schema>

Example 2:

...

score, level, lives and player are mandatory

Since it does not make sense to save a game without a score)? That , 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 in the below example.

Code Block
...
<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

...

Example 3: negative score is not allowed

What if you wanted automatic validation of the values that are saved in your documents? For example, you would like to have the following rules automatically verified:

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

Code Block
...
<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>

...


...