Skip to content

Add object type guards schema#2627

Merged
hueniverse merged 1 commit intohapijs:masterfrom
petrenkoVitaliy:feature/add-object-typeguards-schema
Jul 11, 2021
Merged

Add object type guards schema#2627
hueniverse merged 1 commit intohapijs:masterfrom
petrenkoVitaliy:feature/add-object-typeguards-schema

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