import { array , DecoderError , fields , number , string } from ".." ; expect . addSnapshotSerializer ( { test : ( value ) : boolean => typeof value === "string" && value . includes ( "At root" ) , print : String , } ) ; test ( "tolerant decoding" , ( ) => { const productDecoder = fields ( ( field ) => ( { id : field ( "id" , number ) , name : field ( "name" , string ) , description : field ( "description" , string , { mode : { default : "" } } ) , } ) ) ; const data = { id : 123 , name : "Apple" , description : { html : "<p>Delicious fruit.</p>" } , } ; const errors = [ ] ; const product = productDecoder ( data , errors ) ; expect ( product ) . toMatchInlineSnapshot ( `
    Object {
      "description": "",
      "id": 123,
      "name": "Apple",
    }
  ` ) ; expect ( errors . map ( ( error ) => error . format ( ) ) . join ( "\n\n" ) ) . toMatchInlineSnapshot ( `
      At root["description"]:
      Expected a string
      Got: {"html": "<p>Delici…ruit.</p>"}
    ` ) ; for ( const error of errors ) { expect ( error ) . toBeInstanceOf ( DecoderError ) ; } const namesDecoder1 = array ( string , { mode : "skip" } ) ; const namesDecoder2 = array ( string , { mode : { default : "unknown" } } ) ; const list = [ "Alice" , "Bob" , null , "David" , { value : "Edgar" } ] ; const namesErrors = [ ] ; expect ( namesDecoder1 ( list , namesErrors ) ) . toMatchInlineSnapshot ( `
    Array [
      "Alice",
      "Bob",
      "David",
    ]
  ` ) ; expect ( namesErrors . map ( ( error ) => error . format ( ) ) . join ( "\n\n" ) ) . toMatchInlineSnapshot ( `
      At root[2]:
      Expected a string
      Got: null

      At root[4]:
      Expected a string
      Got: {"value": "Edgar"}
    ` ) ; expect ( namesDecoder2 ( list ) ) . toMatchInlineSnapshot ( `
    Array [
      "Alice",
      "Bob",
      "unknown",
      "David",
      "unknown",
    ]
  ` ) ; } ) ;