Skip to content

Commit

Permalink
clean up validation
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jul 9, 2022
1 parent 5c579c4 commit 4b9c1b5
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions js/getGameLevelsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@ function getGameLevelsSchema( numberOfLevels: number ) {
type: 'array',
elementSchema: {
type: 'number',
isValidValue: ( value: number ) => ( Number.isInteger( value ) && value > 0 )

// validation for each level number in the array
isValidValue: ( value: number ) => ( Number.isInteger( value ) && value > 0 && value <= numberOfLevels )
},
defaultValue: Array.from( { length: numberOfLevels }, ( _, i ) => i + 1 ), // [ 1, 2,...,numberOfLevels]

// validation for the array as a whole
isValidValue: ( array: number[] ) => {
return ( array === null ) || (
// at least 1 level must be visible
array.length > 0 &&
// unique level numbers
array.length === _.uniq( array ).length &&
// valid level numbers
_.every( array, element => element > 0 && element <= numberOfLevels ) &&
// sorted by ascending level number
_.every( array, ( value, index, array ) => ( index === 0 || array[ index - 1 ] <= value ) )
);
Expand Down

0 comments on commit 4b9c1b5

Please sign in to comment.