Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add object type guards schema #2627

Merged

Conversation

petrenkoVitaliy
Copy link
Contributor

@petrenkoVitaliy petrenkoVitaliy commented Jun 28, 2021

Fixes part of #2622 with adding objectSchema's type check.

Type validation will check all properties on existence and correct schema.
Extra parameters don't cause errors and there are may be smth missing in the ObjectPropertiesSchema

Usage:

interface ObjType {
  firstName: string;
  lastName: string;
  nestedObject: {
    middleName: string;
  }
}

// no errors
Joi.object<ObjType, true>({
  firstName: Joi.string(),
  lastName: Joi.string(),
  nestedObject: Joi.object<ObjType['nestedObject'], true>({
    middleName: Joi.string()
  })
})

// error -> 'lastName' is missing
Joi.object<ObjType, true>({
  firstName: Joi.string(),
  nestedObject: Joi.object<ObjType['nestedObject'], true>({
    middleName: Joi.string()
  })
})

// error -> 'lastName' has incorrect shema type
Joi.object<ObjType, true>({
  firstName: Joi.string(),
  lastName: Joi.number(),
  nestedObject: Joi.object<ObjType['nestedObject'], true>({
    middleName: Joi.string()
  })
})

@hueniverse
Copy link
Contributor

Can I get a 👍 from someone who knows TS well?

@hueniverse hueniverse added the types TypeScript type definitions label Jul 11, 2021
@hueniverse hueniverse added this to the 17.4.1 milestone Jul 11, 2021
@hueniverse hueniverse self-assigned this Jul 11, 2021
@hueniverse hueniverse merged commit 13006ec into hapijs:master Jul 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
types TypeScript type definitions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants