title: Angular 13: 'AbstractControl.parent is possibly null' while trying to create a custom validator for a FormGroup
body: <p>I have a form which contains a set of checkboxes and I want to create a custom validator that forces to check at least one of them.</p>
  <p>In <code>form.component.ts</code>:</p>
  <pre><code>
    form: FormGroup = new FormGroup({
      customer: new FormControl('', [Validators.required]),
      partTimeidentification: new FormControl('', [Validators.required]),
      inspectionType: new FormGroup({
        ocr: new FormControl(false, []),
        anomalyDetection: new FormControl(false, []),
        metrics: new FormControl(false, []),
        wiresInspection: new FormControl(false, []),
        other: new FormControl(false, []),
      }, { validators: atLeastOneValidator() }),
      cameraSettings: new FormGroup({
        model: new FormControl('', [Validators.required]),
        size: new FormControl('', [Validators.required]),
        speedOb: new FormControl('', [Validators.required]),
        flashColor: new FormControl('', [Validators.required]),
      }),
    });
  </code></pre>
  <p>In <code>form.component.html</code> (I omitted fields to keep it as clear as possible, you have the structure up here):</p>
  <pre><code> &lt;form class=&quot;flex-col text-center&quot; [formGroup]=&quot;form!&quot;&gt;
       (more fields here)
       &lt;app-box [title]=&quot;'Inspection type'&quot; class=&quot;text-center me-2&quot; [fit_height]=&quot;true&quot; formGroupName=&quot;inspectionType&quot;&gt;
        &lt;div class=&quot;form-check&quot;&gt;
         &lt;input class=&quot;form-check-input&quot; type=&quot;checkbox&quot; formControlName=&quot;ocr&quot; name=&quot;ocr&quot; value=&quot;ocr&quot;/&gt;
         &lt;label class=&quot;form-check-label&quot;&gt; OCR &lt;/label&gt;
        &lt;/div&gt;
        (more checkboxes)
      &lt;/app-box&gt;
      (more fields)
      &lt;button [disabled]=&quot;!form!.valid&quot; type=&quot;button&quot; class=&quot;btn bg-primary-tb text-white mt-3 col-4&quot; (click)=&quot;submit()&quot;&gt;Continue &lt;/button&gt;
   &lt;/form&gt;
  </code></pre>
  <p><code>atLeastOneValidator.directive.ts</code>:</p>
  <pre><code>import { AbstractControl, ValidationErrors, ValidatorFn } from &quot;@angular/forms&quot;;
  
  export function atLeastOneValidator(): ValidatorFn {
      return (group: AbstractControl): ValidationErrors | null =&gt; {
                  let controls = group.parent.controls;
                  let theOne = Object.keys(controls).findIndex(key =&gt; controls[key].value !== false);
        
                  if (theOne) return null;
                
                return { 'atLeastOneRequired': 'At least one should be selected'}
          };
  
        
  };
    
  </code></pre>
  <p>Nevertheless, I keep receiving the error message <code>error TS2531: Object is possibly 'null'.</code> in the validator's <code>group.parent</code>.</p>
  <p>To put the validator's code into words: I want to access the parent (what I guess it is <code>inspectionType</code>, a <code>FormGroup</code>) and then look if any of the children (<code>ocr</code>, <code>anomalyDetection</code>... any of those <code>FormControl</code>s) are set to true, in that case the form is valid.</p>