> ## Documentation Index
> Fetch the complete documentation index at: https://docs.commonality.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Check object

export const CheckContext = ({defaultPath, defaultRelativePath, children}) => <ParamField path="context" type="CheckContext">
    An object containing metadata about the package the check is being run against.

    <Expandable title="check-context-properties">
      {children ?? <>
          <ParamField path="tags" type="string[]" default={[]}>
            The tags that are applied via package configuation to the package being checked
          </ParamField>
          <ParamField path="codeowners" type="string[]" default={[]}>
            The codeowners for the package currently being checked
          </ParamField>
        </>}

      <ParamField path="package" type="PackagePaths">
        An object containing paths to the root directory of the package being checked.
        <Expandable title="package-properties">
          <ParamField path="path" type="string">
            A fully qualified path to the package directory
          </ParamField>
          <ParamField path="relativePath" type="string">
            A relative path to the package directory relative to the root of the project
          </ParamField>
        </Expandable>
      </ParamField>


      <ParamField path="rootPackage" type="PackagePaths">
        An object containing paths to the root directory of the current project.

        <Expandable title="package-properties">
          <ParamField path="path" type="string">
            A fully qualified path to the package directory
          </ParamField>
          <ParamField path="relativePath" type="string">
            A relative path to the package directory relative to the root of the project
          </ParamField>
        </Expandable>
      </ParamField>

      <ParamField path="allPackages" type="PackagePaths[]">
        An array of objects containing paths to every package directory in the current project.
        <Expandable title="package-properties">
          <ParamField path="path" type="string">
            A fully qualified path to the package directory
          </ParamField>
          <ParamField path="relativePath" type="string">
            A relative path to the package directory relative to the root of the project
          </ParamField>
        </Expandable>
      </ParamField>

    </Expandable>

  </ParamField>;

## Properties

### `message`

**Required**

A string that will be shown as the default title for the check when running the CLI and Commonality Studio.

### `validate`

**Required**

The validate function is used to set the check's status to `pass`, `warn`, or `fail`.

The `validate` function will be run against all packages matching a [selector](/selectors).

This function can be asynchronous.

#### Parameters

<CheckContext />

#### Returns

If the function returns `true`, the check will be set to `pass`.
If the function returns any other value, the check will be set to `warn` or `fail` based on the configured [level](#level).

<Tabs>
  <Tab title="TypeScript">
    ```ts theme={null}
    import type { Check } from 'commonality';

    export default {
      message: 'Package has a CODEOWNER',
      validate: (ctx) => Boolean(ctx.codeowners.length),
    } satisfies Check;
    ```
  </Tab>

  <Tab title="JavaScript">
    ```js theme={null}
    export default {
      message: 'Package has a CODEOWNER',
      validate: (ctx) => Boolean(ctx.codeowners.length),
    };
    ```
  </Tab>
</Tabs>

Returning a message object allows you to modify the check's default message and provide additional context about the status of the check.

<Tabs>
  <Tab title="TypeScript">
    ```ts theme={null}
    import type { Check } from 'commonality';
    import { json, diff } from 'commonality';

    export default {
      message: 'Package has internal config',
      validate: (ctx) => {
        // ...validation logic

        if (fileExists) {
          return {
            // This message will override the default message
            message: 'File "internal-config.json" does not exist',
            // This path will be shown directly underneath the message for quicker debugging
            path: ctx.package.path,
            // This will be shown as a suggestion for how to fix the issue
            suggestion: diff(source, expected)
          };
        }
      },
    } satisfies Check;
    ```
  </Tab>

  <Tab title="JavaScript">
    ```ts theme={null}
    import { json, diff } from 'commonality';

    export default {
      message: 'Package has internal config',
      validate: (ctx) => {
        // ...validation logic

        if (fileExists) {
          return {
            // A string that will override default message for the check
            message: 'File "internal-config.json" does not exist',
            // A string representing a path that will be shown directly underneath the check's message.
            path: ctx.package.path,
            // A string representing a suggestion for how to fix a failed check
            suggestion: diff(source, expected)
          };
        }
      },
    }
    ```
  </Tab>
</Tabs>

### `fix`

A check's `fix` function will only run on packages that where the check's [validate](#validate) function has returned a falsey value. This function should update packages so that they pass the check's [validate](#validate) function.

This function can be asynchronous.

<Tip> We provide helper utilities like [json](/reference/json) and [text](/reference/text) to make it easier to read and write to files in [fix](/reference/check-object#fix) functions.</Tip>

#### Parameters

<CheckContext />

<Tabs>
  <Tab title="TypeScript">
    ```ts theme={null}
    import type { Check } from 'commonality';
    import { json } from 'commonality';

    export default {
      return {
        //...
        fix: async (ctx) => {
          const tsConfig = await json(ctx.package.path, 'tsconfig.json').get();

          if (!tsConfig) {
            return;
          }

          await tsConfig.merge({ extends: base });
        },
      };
    } satisfies Check;
    ```
  </Tab>

  <Tab title="JavaScript">
    ```ts theme={null}
    import { json } from 'commonality';

    export default {
      return {
        //...
        fix: async (ctx) => {
          const tsConfig = await json(ctx.package.path, 'tsconfig.json').get();

          if (!tsConfig) {
            return;
          }

          await tsConfig.merge({ extends: base });
        },
      };
    };
    ```
  </Tab>
</Tabs>

### `level`

A string that can be set to "warning" or "error".

If set to `"error"`, the CLI will exit with a non-zero exit code if this check is ever invalid. Default is `"warning"`.
