New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

indicative-utils

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

indicative-utils

Utilities used by other Indicative packages

  • 7.0.0
  • npm
  • Socket score

Version published
Weekly downloads
10K
increased by35.36%
Maintainers
1
Weekly downloads
 
Created
Source

UsageWhy?FeaturesExamplesDocumentation


Utils

Handy utility functions used by other Indicative packages. If you are extending Indicative to add your own rules, then you may find some handy utilities in this package.

Usage

Install the package from npm

npm i indicative-utils

and then import the functions you need

import { cast, ensureLength, skippable, patchValue } from 'indicative-utils'

API

Following is the usage for individual methods

cast(value: any, toType: string, errorMessage?: string): output

Casts value to a given data type. Returns null when unable to cast value.

cast('10', 'number', 'Optional error message to raise exception')
  • value: The value to cast
  • toType: One of the available types. 'number' | 'date' | 'string' | 'integer' | 'boolean'
  • errorMessage (optional): Raises an exception when errorMessage is defined and unable to cast value.

ensureLength (args: any[], errorMessage: string, minLength = 1): void

Raise an exception when the length of args is less than the defined minLength.

ensureLength(['foo', 'bar'], 'Array must have minimum of 3 items', 3)
  • args: Array to test length
  • errorMessage: Message for exception, when array length is less than the desired length.
  • minLength (default = 1): Minimum desired length.

patchValue (data: any, field: string, fieldValue: any, root: DataRoot)

This method is used by validation functions that wants to mutate/cast the input value and set it back on the source object. It is recommended to use this method vs doing it manually, since it handles edge cases.

The DataRoot value is available inside all validation functions, you must pass it as it is.

patchValue({ age: '10' }, 'age', 10, {})
skippable(value: any, fieldName: string, config: { existyStrict: boolean })

Again, this method is used by validation functions to know if the value of field must be skipped from validation or not.

Indicative by default assumes all fields as optional and one must use required rule for required fields. Which means that validation rules like email, date and so on must skip undefined or null values based upon the user config. This method does that check for you.

// read field value inside validation function
const fieldValue = data[field]
skippable(fieldValue, field, config)

Keywords

FAQs

Package last updated on 01 Apr 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc