How to use the debrief.annotate function in debrief

To help you get started, we’ve selected a few debrief examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nvie / decoders / src / array.js View on Github external
clone.splice(
                index,
                1,
                annotate(ann, ann.annotation !== undefined ? `${ann.annotation} (at index ${index})` : `index ${index}`)
            );

            // const errValue = [];
            // if (index > 0) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            // errValue.push(
            // );
            // if (index < iterable.length - 1) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            return Err(annotate(clone));
        }
        index++;
    }
    return Ok(results);
}
github nvie / decoders / src / array.js View on Github external
function all(iterable: Array>, blobs: Array): DecodeResult> {
    const results: Array = [];
    let index = 0;
    for (const result of iterable) {
        try {
            const value = result.unwrap();
            results.push(value);
        } catch (ann) {
            // Rewrite the annotation to include the index information, and inject it into the original blob
            const clone = [...blobs];
            clone.splice(
                index,
                1,
                annotate(ann, ann.annotation !== undefined ? `${ann.annotation} (at index ${index})` : `index ${index}`)
            );

            // const errValue = [];
            // if (index > 0) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            // errValue.push(
            // );
            // if (index < iterable.length - 1) {
            //     errValue.push('...'); // TODO: make special mark, not string!
            // }
            return Err(annotate(clone));
        }
        index++;
    }
    return Ok(results);
github nvie / decoders / src / array.js View on Github external
export const poja: Decoder> = (blob: mixed) => {
    if (!Array.isArray(blob)) {
        return Err(annotate(blob, 'Must be an array'));
    }
    return Ok(
        // NOTE: Since Flow 0.98, Array.isArray() returns $ReadOnlyArray
        // instead of Array.  For rationale, see
        // https://github.com/facebook/flow/issues/7684.  In this case, we
        // don't want to output read-only types because it's up to the user of
        // decoders to determine what they want to do with the decoded output.
        // If they want to write items into the array, that's fine!
        // The fastest way to turn a read-only array into a normal array in
        // Javascript is to use .slice() on it, see this benchmark:
        // http://jsben.ch/lO6C5
        blob.slice()
    );
};
github nvie / decoders / src / object.js View on Github external
}
        }

        // Deal with errors now. There are two classes of errors we want to
        // report.  First of all, we want to report any inline errors in this
        // object.  Lastly, any fields that are missing should be annotated on
        // the outer object itself.
        const fieldsWithErrors = Object.keys(fieldErrors);
        if (fieldsWithErrors.length > 0 || missing.size > 0) {
            let err;

            if (fieldsWithErrors.length > 0) {
                const errorlist = fieldsWithErrors.map(k => [k, fieldErrors[k]]);
                err = annotateFields(blob, errorlist);
            } else {
                err = annotate(blob);
            }

            if (missing.size > 0) {
                const errMsg = Array.from(missing)
                    .map(key => `"${key}"`)
                    .join(', ');
                const pluralized = missing.size > 1 ? 'keys' : 'key';
                err = annotate(err, `Missing ${pluralized}: ${errMsg}`);
            }

            return Err(err);
        }

        return Ok(record);
    });
}
github nvie / decoders / src / date.js View on Github external
export const date: Decoder = (value: mixed) =>
    isDate(value) ? Ok(((value: cast): Date)) : Err(annotate(value, 'Must be a Date'));
github nvie / decoders / src / constants.js View on Github external
export const undefined_: Decoder = (blob: mixed) =>
    blob === undefined ? Ok(blob) : Err(annotate(blob, 'Must be undefined'));
github nvie / decoders / src / boolean.js View on Github external
export const boolean: Decoder = (blob: mixed) => {
    return typeof blob === 'boolean' ? Ok(blob) : Err(annotate(blob, 'Must be boolean'));
};
github nvie / decoders / src / constants.js View on Github external
export const null_: Decoder = (blob: mixed) => (blob === null ? Ok(blob) : Err(annotate(blob, 'Must be null')));
github nvie / decoders / src / number.js View on Github external
export const anyNumber: Decoder = (blob: anything) => {
    return typeof blob === 'number' && !Number.isNaN(blob) ? Ok(blob) : Err(annotate(blob, 'Must be number'));
};

debrief

Object serialization and annotation, for use in human-friendly error messages

MIT
Latest version published 4 years ago

Package Health Score

45 / 100
Full package analysis