0

I am processing some data in my backend (written in Typescript) provided via a form in html. Before writing the data to the database, I perform validations using type guards.

let myObject = { property: parseProperty(property) } 

where the type definition for "property" is given as:

interface PropertyType = { 
    Field1: string; 
    Field2: string; 
  } 

The property object is first parsed via the following type-guard:

const parseProperty = (property: unknown): PropertyType => {
  if (!property || !isPropertyEntry(property)) {
    throw new Error("Incorrect or missing property entry")
  }
  return property; 

Then we use the typeguard below (which is incomplete) to validate property's objects fields.

const isPropertyEntry = (property : unknown): property is PropertyType => {
  if( !('Field1' in property) || !('Field2' in property) ) {    // <--- invalid statement
    throw new Error("Incorrect or missing field/s in property");
  } 
  ...
  ...

}

TS throws an error for these two statements: 'Field1' in property & 'Field2' in property: Object is of type 'unknown'.ts(2571)

My question is how would I do a proper validation of the object 'property' and make sure Field1 & Field2 exist and are correct type?

1
  • Use the PropertyType interface instead of unknown, or supply it via a generic type argument (e.g. function isPropertyEntry<T>(property: T) ... Commented Aug 22, 2022 at 12:18

1 Answer 1

1

It does not look like you want to have a type guard. Since you want to throw an Error when property is not a PropertyType, you probably need an assertion function.

function assertIsPropertyEntry(property: any): asserts property is PropertyType {
  if (
    typeof property !== "object" 
    || !property 
    || !('Field1' in property) 
    || !('Field2' in property)
    || typeof property.Field1 !== "string"
    || typeof property.Field2 !== "string"
  ) {
    throw new Error("Incorrect or missing field/s in property");
  } 
}

const parseProperty = (property: unknown): PropertyType => {
  assertIsPropertyEntry(property)
  return property
}

Playground

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.