Skip to content

validateIconSet()

This function is part of Iconify Utils package.

Function validateIconSet() validates icon set, returning cleaned up IconifyJSON object.

If there are errors in provided data:

  • If error is critical, function will throw an exception.
  • If error can be fixed, function will throw an exception, unless fix option is provided.

This function checks everything, including metadata.

Usage

Function has the following parameters:

  • data, object. Data to validate.
  • options, object. Optional options object.

Function returns:

  • IconifyJSON object on success (same as passed in first parameter).

If function encounters an error that cannot be fixed, function will throw an exception.

Options

Options object has the following properties:

  • fix, boolean. If set to true, function will attempt to fix errors whenever possible instead of throwing exception. Default value is false.
  • prefix, string. Default value for prefix property of icon set. If set, function will overwrite prefix in icon set with your value.
  • provider, string. Default value for provider property of icon set. If set, function will overwrite provider in icon set with your value.

Example

usage.ts
tsimport { promises as fs } from 'fs';
import { validateIconSet } from '@iconify/utils';

(async () => {
   // Load data from file
   const data = JSON.parse(
       await fs.readFile(
           './node_modules/@iconify-json/mdi-light/icons.json',
           'utf8'
       )
   );

   // Validate data
   const iconSet = validateIconSet(data);

   // Count icons (simple calculation)
   const count =
       Object.keys(iconSet.icons).length +
       (iconSet.aliases ? Object.keys(iconSet.aliases).length : 0);
   console.log(`Found ${count} icons`);
})();

Quick validation

This function is quite big, so it is not recommended to bundle it for browser usage.

If you want to validate icon set in browser, such as when loading data from API in an icon component, use quicklyValidateIconSet() instead. It is smaller and faster because it does not check metadata, does not attempt to fix errors.

Released under the Apache 2.0 License.