Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get JSON.parse result covered by the Flow type checker?

Tags:

json

flowtype

I'm new to working with flow. I'm trying to get as close to 100% flow coverage as possible on a project, and one thing I can't figure out is how to handle is JSON.parse.

type ExampleType = {
  thingOne: boolean,
  thingTwo: boolean,
};
const exampleVariable: ExampleType = JSON.parse(
  '{thingOne: true, thingTwo: false}'
);

So I have a type, and I receive a string from another source, and I parse it and expect it to be of that type.

The whole JSON.parse(...) section is marked as "Not covered by flow".

Is there a way to get a file to 100% flow coverage if JSON.parse is used in that file? How? What exactly is flow saying when it says that line isn't covered?

like image 354
fnsjdnfksjdb Avatar asked Nov 16 '17 17:11

fnsjdnfksjdb


People also ask

How do I parse JSON response in Servicenow?

Creating a Script to Parse and Map DataParse the JSON response with the JSON. parse() method. Map keys from the response body to the output variables. (function execute(inputs, outputs) { var responseObject = JSON.

How do I access parsed JSON data?

To access the JSON object in JavaScript, parse it with JSON. parse() , and access it via “.” or “[]”.

How do I parse a JSON flow in Salesforce?

You can use JSON. desrialize to parse your JSON. Make sure your structure class contains at least one member variable with the invocable variable annotation. Return the deserialized value back to apex.


1 Answers

The problem is that JSON.parse returns any. Here is the signature:

static parse(text: string, reviver?: (key: any, value: any) => any): any;

Flow cannot guarantee that the assignment of the parse result to the type ExampleType is correct because who knows what will come out when you parse incoming JSON?

But you can get coverage to 100% if you parse with flow-validator instead. When parsing a string as far as Flow knows that string could have come from anywhere. So there is no static guarantee that the JSON data in the string has the shape that you expect. What flow-validator does it to provide an API to describe a validation schema for your data instead of a type. The schema is checked at runtime while parsing. Flow-validator automatically generates a static type from your schema, and assigns the result from a successful parse to that type. Here is what your example looks like with flow-validator:

import { boolean, object } from "flow-validator"

const ExampleSchema = object({
  thingOne: boolean,
  thingTwo: boolean
})

const exampleVariable = ExampleSchema.parse(
  '{"thingOne": true, "thingTwo": false}'
)

You can check and see that Flow infers the correct type for exampleVariable, and your Flow coverage is now at 100%. If the JSON data does not have the correct shape then ExampleSchema.parse will throw an error.

You can get a type from the schema like this:

type ExampleType = typeof ExampleSchema.type

This version of ExampleType is just like the one in your original example. Extracting a type automatically saves you from having to write the shape for your data structure twice, and it also guarantees that the static type stays in sync with the runtime validation schema.

like image 190
Jesse Hallett Avatar answered Nov 15 '22 05:11

Jesse Hallett