0.0.0 • Published 2 years ago
rx-xtra.safe-map v0.0.0
Rx Xtra: SafeMap
safeMap allows you to perform data validation and conversion with one operator.  Great for use with JSON.parse and Zod.
If the validation / conversion throws an error, then the emitted signal is ignored, but the Observable continues.
rx-xtra.safe-map is part of Rx Xtra, a collection of RxJS utilities.
Created by Joel Shinness LinkTree • Github • Buy me a coffee!
Usage
safeMap<A, B>
- Parameters- project:- (value:A, index:number) => Bconverts a value of type- Ainto a value of type- B. If this function throws an error, then the item is ignored.
- onError:- (err:any) => voidgets called with the validation error if one exists. If this function throws an error, then the entire Observable errors.
 
- Returns
import { from } from 'rxjs';
import { z } from 'zod';
import { safeMap } from 'rx-xtra.safe-map';
const data = [
  'Not JSON',
  '{"error": "invalid JSON"}',
  '{"msg": "Valid Object"}',
];
const template = z.object({
  'msg': z.string()
});
function validate(input:string):z.infer<typeof template>{
  const parsedJSON = JSON.parse(input);
  const validObject = template.parse(parsedJSON);
  return validObject;
}
from(data).pipe(
  safeMap(
    validate, 
    (err) => { console.log('Validation Error:', err.message); 
  })
).subscribe((val) => { console.log('Next:', val.msg); });
// OUTPUT:
// Validation Error: Unexpected token 'N', "Not JSON" is not valid JSON
// Validation Error: [
//   {
//     "code": "invalid_type",
//     "expected": "string",
//     "received": "undefined",
//     "path": [
//       "msg"
//     ],
//     "message": "Required"
//   }
// ]
// Next: Valid Object0.0.0
2 years ago