Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

untitled notebook

node v12.22.12
version: 1.0.0
endpointsharetweet
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)
Loading…

no comments

    sign in to comment