fromJSON

Parses a JavaScript object previously serialised as aValidation.toJSON() into a proper Validation structure.

This feature is experimental!

This API is still experimental, so it may change or be removed in future versions. You should not rely on it for production applications.

Signature

fromJSON(value, parsers = { [typeName]: adt }, keysIndicateType = false)
type JSONSerialisation = {
  "@@type":  String,
  "@@tag":   String,
  "@@value": Object Any
}
type JSONParser = {
  fromJSON: (JSONSerialisation, Array JSONParser) => Variant
}

(JSONSerialisation, Array JSONParser) => Variant

Documentation

Parses a JavaScript object previously serialised as aValidation.toJSON() into a proper Validation structure.

See the docs for adt/union/derivations/serialization for more details.

Example:

const Validation = require('folktale/validation');

Validation.fromJSON(Validation.Success(1).toJSON());
// ==> Validation.Success(1)

Validation.fromJSON(Validation.Failure(1).toJSON());
// ==> Validation.Failure(1)

Properties

Source Code

function(value, parsers = { [typeName]: adt }, keysIndicateType = false) {
    const valueTypeName = value[typeJsonKey];
    const valueTagName = value[tagJsonKey];
    const valueContents = value[valueJsonKey];
    assertType(typeName, valueTypeName);
    const parsersByType = keysIndicateType ? parsers
          : /*otherwise*/                    indexByType(values(parsers));

    const parsedValue = mapValues(valueContents, parseValue(parsersByType));
    return extend(Object.create(adt[valueTagName].prototype), parsedValue);
  }
Stability
experimental
Licence
MIT
Authors
Copyright
(c) 2013-2017 Quildreen Motta, and CONTRIBUTORS
Authors
  • @boris-marinov
Maintainers
  • Quildreen Motta <queen@robotlolita.me> (http://robotlolita.me/)