const {Validation, valid, invalid} = require("@rexform/validation")
const {isNil, isEmpty, complement, either, includes, flip, both, lte, gte, pipe, map} = require('ramda')
const videogames = [
{id: 1, name: 'Doom', genre: 'FPS', rating: 7},
{id: 2, name: 'NieR: Automata', genre: 'Action RPG', rating: 100},
{id: 3, name: 'Dead Cells', genre: 'Rogue-like', rating: 8},
]
const validGenres = ['FPS', 'Platforms', 'RPG', 'Strategy', 'Simulator']
const isFilled = complement(either(isNil, isEmpty))
const isBetween = (a, b) => both(flip(lte)(b), flip(gte)(a))
const nameDecoder = Validation.fromPredicateOr(() => 'name can not be empty', isFilled)
const genreDecoder = Validation.fromPredicateOr(() => 'genre must be in validGenres', flip(includes)(validGenres))
const ratingDecoder = Validation.fromPredicateOr(() => 'rating must be between 1 and 10', isBetween(1, 10))
const videogameDecoder = pipe(
Validation.validateProperties({
id: valid,
name: nameDecoder,
genre: genreDecoder,
rating: ratingDecoder,
}),
Validation.allProperties,
videogame => videogame.mapError(e => `In ID=${videogame.value.id}: ${e}`),
)
const videogameArrayDecoder = pipe(map(videogameDecoder), Validation.sequence)
videogameArrayDecoder(videogames)