How do I check if 'key' is correctly populated with 'known' values in JSON file

check voter registration status
how can i check my voter history
how to find out if someone is a registered democrat or republican
can i vote
am i registered to vote texas
check voter registration california
register to vote
am i registered as a democrat

I am trying to check if a particular key is assigned with only a set of values. This values are listed as an enum in Typescript.

Please note, I do that want to check the values directly like it explained below, but would like to check the enum type.

Check if key/value is in JSON

I need to check only known regions are used in the json file.

export type Regions = Na | Emea | Apac;

export interface Na {
    NA: "na";
}

export interface Emea {
    EMEA: "emea";
}

export interface Apac {
    APAC: "apac";
}

I need to write a function similar to below which check of only known values are used for the key Region

function isValidRegion(candidate: any): candidate is Regions {
 // if (candidate is one the type of Regions
 // console.log("Regions valid");
 // else
 // console.log("invalid Regions are used in input JSON");
 result = candidate;
 return result;
}

Do something like this:

function isValidRegion(candidate: any): candidate is Regions {
      return Object.keys(Regions).some(region => region === candidate)
}

Where's My Refund? Check the Status of My Tax Return, Find out if the IRS received your tax return and check the status of your refund. Learn why your tax refund could be lower than you expected. To check for identity theft to lessen the damage, you need to identify and address the issue as quickly as possible by looking for signs of fraud, protecting your personal information and taking other important steps. Read on to learn how to find out if you're a victim of identity theft and what you can do if you suspect you're a victim.

If I understand correctly, you want to validate some JSON data as if it were Typescript and check if the values therein match the interfaces you provide. In general, this is impossible without using Typescript itself, luckily TS provides the compiler API which we can use in our own programs. Here's the minimal example:

Let myjson.ts (which describes your types) be:

type Name = 'a' | 'b' | 'c';

export default interface MyJson {
    names: Name[]
    values: number[]
}

Then you can write something like:

import * as ts from "typescript";
import * as fs from 'fs';

function compile(fileNames: string[], options: ts.CompilerOptions): string[] {
    let program = ts.createProgram(fileNames, options);
    let emitResult = program.emit();

    return ts
        .getPreEmitDiagnostics(program)
        .concat(emitResult.diagnostics)
        .map(d => ts.flattenDiagnosticMessageText(d.messageText, ' '));
}

function validate(someJson: string): boolean {
    let source = `
        import MyJson from "./myjson";
        let x: MyJson = ${someJson};
    `;
    fs.writeFileSync('tmp.ts', source, 'UTF-8');
    let errors = compile(['tmp.ts', 'myjson.ts'], {});
    if (errors.length)
        console.log(errors.join('\n'));
    return errors.length === 0;

}

///

let goodJson = '{ "names": ["a", "b"], "values": [1,2,3] }';
let badJson  = '{ "names": ["a", "b", "x"], "values": "blah" }';

console.log('ok', validate(goodJson));
console.log('ok', validate(badJson));

The result will be

ok true
Type '"x"' is not assignable to type 'Name'.
Type 'string' is not assignable to type 'number[]'.
ok false

Check Your Service and Support Coverage, Check your Apple warranty status. Enter a serial number to review your eligibility for support and extended coverage. Do I need to do anything to get a stimulus check? If you fit the criteria above, then you don’t need to do anything to get a stimulus check. You will get a stimulus check automatically.

I achieved this with the following code:

enum Regions {
NA = "na",
EMEA = "emea",
APAC = "apac"
}

const possibleRegions = [Regions.NA, Regions.EMEA, Regions.APAC];

private isValidRegion(value) 
{
    return value !== null && value.Region !== null && possibleRegions.indexOf(value.Region) > -1;
}

Check Your Voter Registration Status, It takes less than 30 seconds to check your registration status with our free 50-​state tool. And if you're not registered, we'll help you take care of that too. Fast, free  Windows computers sometimes do get viruses and other malware, but not every slow or misbehaving PC is infected by malware. Here’s how to check if you actually have a virus—and whether that suspicious process is dangerous or not.

Check if a vehicle is taxed, Check and report if a vehicle has up-to-date vehicle tax or is 'off road' (SORN) How to call the IRS to ask about your late stimulus check . If you do reach out to the IRS over the phone, it's a good idea to give yourself plenty of time for the call. We wouldn't be surprised

Check the MOT history of a vehicle, Check the past results of a vehicle's MOT tests, including: if it passed or failed; the mileage recorded when it was tested; where each test was done; what parts  Head to the IRS' Get My Payment page and tap the blue Get My Payment button to check the status of your economic impact payment. 2. On the next page about authorized use, tap OK .

Check for Recalls: Vehicle, Car Seat, Tire, Equipment, Use our VIN lookup tool to check for recalls on your car, or search by make and model. Also, get recall information on car seats, tires and equipment. Check verification services can also keep records of your check writing activity. Merchants subscribe to services that alert them to potential problems before they accept a check from a customer. TeleCheck, Certegy, Early Warning Systems, and CrossCheck are other popular databases for merchants, banks, and credit unions.

Comments
  • Thank you @georg for your answer.