Skip to content

Instantly share code, notes, and snippets.

@percursoaleatorio
Last active February 18, 2016 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save percursoaleatorio/0512f97c6e7bbb2aa426 to your computer and use it in GitHub Desktop.
Save percursoaleatorio/0512f97c6e7bbb2aa426 to your computer and use it in GitHub Desktop.
(: Suppose that validate-record is a function that checks a single record.
It receives the sequence of records to be validated and the position of the specific record to validate.
It returns a sequence.
If the recork is OK, then the returned sequence is empty.
If the recork is not OK, then the returned sequence contains a single element with the validation result.
Something like... :)
declare function validate-record($records as element()+, $position as xs:integer) as element()?
{ (: Whatever you need to do the check the record in that position.
Return an empty sequence if the record is ok.
Else return a sequence the "error element" you created. :) };
(: Suppose that validate-sequence is a function that checks the full sequence of records.
It receives the sequence of records to be validated, a position in that sequence,
the sequence of errors already found and the maximum number of errors we what to report.
If there are still records to check and the maximum number of errors hasn't been reached,
then the the function is called again for the next record, passing it the current error sequence union the result of the check in the current record.
Else the function just returns the error sequence it received.
Something like... :)
declare function validate-sequence($records as element()+, $position as xs:integer, $errors as element()*, $maxErrors as xs:integer) as element()?
{
if (($position <= count($records)) and ($maxErrors < count($errors)))
then validate-sequence($records, $position+1, $errors | validate-record($records,$position),$maxErrors)
else return $errors
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment