Skip to content

afeiship/next-defaults-validate

Repository files navigation

next-defaults-validate

Get defaults by validate function.

version license size download

installation

yarn add @jswork/next-defaults-validate

usage

import '@jswork/next-defaults-validate';

const target = {
  username: 'xiaoming',
  email: 'abc',
  other: 'value',
  nested: {
    company: {
      address: null
    }
  }
};

nx.defaultsValidate(target, {
  username: (value) => value.length > 3 ? null : 'anonymous',
  email: (value) => value.includes('@') ? null : '[email protected]',
  'nested.company.address': (value) => value ? null : 'unknown'
});

// output: 
/*
{
  username: 'xiaoming',
  email: '[email protected]',
  other: 'value',
  nested: {
    company: {
      address: 'unknown'
    }
  }
}
*/

license

Code released under the MIT license.