Repository: sindresorhus/ow
Pull request #211 (2021-06-02 03:42:22)

Fix `ow.object` to return `ArgumentError` when `null` or `undefined` is passed
Fix: https://github.com/sindresorhus/ow/issues/201

sindresorhus: Can you rebase from the main branch and ensure the tests passes?
wafuwafu13: https://github.com/sindresorhus/ow/pull/211/checks?check_run_id=2727334806

>Error: /home/runner/work/ow/ow/source/index.ts: line 37, col 2, Error - Member call should be declared before all field definitions. (@typescript-eslint/member-ordering)

Fix https://github.com/sindresorhus/ow/pull/211/commits/ef3a5c554c4725da81ece440b459b3ec38ff3e18
wafuwafu13: `object.ts › object.hasKeys` is also failed 🤔 

https://github.com/sindresorhus/ow/pull/211/checks?check_run_id=2732547395

<details>

<summary>result</summary>

```zsh
  object.ts › object.instanceOf

  /home/runner/work/ow/ow/test/object.ts:164

   163:                                             
   164:   t.throws(() => {                          
   165:     ow(null, ow.object.instanceOf(Unicorn));

  Function threw unexpected exception:

  ArgumentError {
    validationErrors: Map {
      'object' => Set {
        'Expected argument to be of type `object` but received type `null`',
        'Expected object `null` to be of type `Unicorn`',
      },
    },
    message: `Expected argument to be of type `object` but received type `null`␊
    Expected object `null` to be of type `Unicorn``,
  }

  Expected message to equal:

  'Expected object `null` to be of type `Unicorn`'

  Object.ow [as default] (source/index.ts:3:107)
  test/object.ts:165:5
  test/object.ts:164:4



  object.ts › object.hasKeys

  /home/runner/work/ow/ow/test/object.ts:190

   189:                                                                  
   190:   t.throws(() => {                                               
   191:     ow({unicorn: '🦄'}, ow.object.hasKeys('unicorn', 'rainbow'));

  Function threw unexpected exception:

  ArgumentError {
    validationErrors: Map {
      'object' => Set {
        'Expected object to have keys `["rainbow"]`',
      },
    },
    message: 'Expected object to have keys `["rainbow"]`',
  }

  Expected message to equal:

  'Expected object `Unicorn` to have keys `["rainbow"]`'

  Object.ow [as default] (source/index.ts:3:107)
  test/object.ts:191:5
  test/object.ts:190:4
```

</details>
wafuwafu13: `object.ts › object.hasKeys` is also failed 🤔 

https://github.com/sindresorhus/ow/pull/211/checks?check_run_id=2734089247

<details>

<summary>result</summary>

```zsh
  object.ts › object.instanceOf

  /home/runner/work/ow/ow/test/object.ts:164

   163:                                             
   164:   t.throws(() => {                          
   165:     ow(null, ow.object.instanceOf(Unicorn));

  Function threw unexpected exception:

  ArgumentError {
    validationErrors: Map {
      'object' => Set {
        'Expected argument to be of type `object` but received type `null`',
        'Expected object `null` to be of type `Unicorn`',
      },
    },
    message: `Expected argument to be of type `object` but received type `null`␊
    Expected object `null` to be of type `Unicorn``,
  }

  Expected message to equal:

  'Expected object `null` to be of type `Unicorn`'

  Object.ow [as default] (source/index.ts:3:107)
  test/object.ts:165:5
  test/object.ts:164:4



  object.ts › object.hasKeys

  /home/runner/work/ow/ow/test/object.ts:190

   189:                                                                  
   190:   t.throws(() => {                                               
   191:     ow({unicorn: '🦄'}, ow.object.hasKeys('unicorn', 'rainbow'));

  Function threw unexpected exception:

  ArgumentError {
    validationErrors: Map {
      'object' => Set {
        'Expected object to have keys `["rainbow"]`',
      },
    },
    message: 'Expected object to have keys `["rainbow"]`',
  }

  Expected message to equal:

  'Expected object `Unicorn` to have keys `["rainbow"]`'

  Object.ow [as default] (source/index.ts:3:107)
  test/object.ts:191:5
  test/object.ts:190:4
```

</details>
wafuwafu13: Fixed tests :) https://github.com/sindresorhus/ow/pull/211/commits/becbd56b4d8ceeee9117631af5d55bc2bbc1e4fb

Patch:
diff --git a/source/predicates/object.ts b/source/predicates/object.ts
index 40a3f2c..c6d348b 100644
--- a/source/predicates/object.ts
+++ b/source/predicates/object.ts
@@ -93,7 +93,7 @@ export class ObjectPredicate<T extends object = object> extends Predicate<T> {
 	instanceOf(instance: Function): this {
 		return this.addValidator({
 			message: (object: object, label: string) => {
-				let {name} = object.constructor;
+				let {name} = object?.constructor ?? {};
 
 				if (!name || name === 'Object') {
 					name = JSON.stringify(object);


Test patch:
diff --git a/test/object.ts b/test/object.ts
index e09b7a4..baf46f5 100644
--- a/test/object.ts
+++ b/test/object.ts
@@ -160,6 +160,14 @@ test('object.instanceOf', t => {
 	t.throws(() => {
 		ow({unicorn: '🦄'}, ow.object.instanceOf(Unicorn));
 	}, 'Expected object `{"unicorn":"🦄"}` to be of type `Unicorn`');
+
+	t.throws(() => {
+		ow(null, ow.object.instanceOf(Unicorn));
+	}, /Expected object `null` to be of type `Unicorn`/);
+
+	t.throws(() => {
+		ow(undefined, ow.object.instanceOf(Unicorn));
+	}, /Expected object `undefined` to be of type `Unicorn`/);
 });
 
 test('object.hasKeys', t => {
@@ -181,7 +189,7 @@ test('object.hasKeys', t => {
 
 	t.throws(() => {
 		ow({unicorn: '🦄'}, ow.object.hasKeys('unicorn', 'rainbow'));
-	}, 'Expected object `Unicorn` to have keys `["rainbow"]`');
+	}, 'Expected object to have keys `["rainbow"]`');
 
 	t.throws(() => {
 		ow({unicorn: {value: '🦄'}}, ow.object.hasKeys('unicorn.foo'));