How to use fastest-validator - 4 common examples

To help you get started, we’ve selected a few fastest-validator examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github MainframeHQ / erebos / packages / rpc-handler / src / index.ts View on Github external
export function normalizeMethods(
  methods: Methods,
  validatorOptions?: any | undefined,
): NormalizedMethods {
  const v = new Validator(validatorOptions)

  return Object.keys(methods).reduce((acc, name) => {
    const method = methods[name]
    if (typeof method === 'function') {
      acc[name] = method
    } else if (typeof method.handler === 'function') {
      if (method.params == null) {
        acc[name] = method.handler
      } else {
        const check = v.compile(method.params)
        acc[name] = function validatedMethod>(
          ctx: C,
          params: P,
        ) {
          // eslint-disable-next-line @typescript-eslint/ban-types
          const checked = check(params as Object)
github tidepool-org / viz / src / utils / validation / schema.js View on Github external
import Validator from 'fastest-validator';
import _ from 'lodash';
import { MGDL_UNITS, MMOLL_UNITS } from '../constants.js';
// import { MGDL_UNITS, MMOLL_UNITS, MS_IN_DAY } from '../constants.js';


const v = new Validator({
  messages: {
    missingFieldDependancy: "Field(s) '{expected}' are expected when field '{field}' exists. Value(s) were {actual}",
  },
});

/* eslint-disable no-underscore-dangle */
v.add('withDependantFields', (value, schema, fieldName, object) => {
  let missingFields = false;

  _.each(schema.fields, field => {
    if (!missingFields) {
      missingFields = _.isNil(object[field]);
    }
  });

  if (missingFields) {
github qmit-pro / moleculer-api / src / interface / validation.ts View on Github external
export function compileValidationRule(rule: ValidationRule | ValidationRule[], opts?: RecursivePartial): ValidationFn {
  const options: ValidateOptions = _.defaultsDeep(opts || {}, { strict: true, field: "value", messages: {} });

  // apply messages option
  const validator = new Validator({ messages: options.messages });

  // apply field option
  const schema = { [options.field]: rule } as ValidationSchema;

  // apply strict option
  schema.$$strict = !options.strict as any;

  // create checker
  const check = validator.compile(schema);
  return (value) => check({ [options.field]: value });
}
github qmit-pro / moleculer-api / src / interface / validation.ts View on Github external
export function compileValidationSchema(schema: ValidationSchema, opts?: RecursivePartial): ValidationFn {
  const options: ValidateOptions = _.defaultsDeep(opts || {}, { strict: true, field: "", messages: {} });

  // apply messages option
  const validator = new Validator({ messages: options.messages! });

  // prepare field (prefix) option
  const prefix = options.field!.trim();
  if (prefix) {
    const prefixedSchema: ValidationSchema = {};
    for (const [k, v] of Object.entries(schema)) {
      prefixedSchema[`${prefix}.${k}`] = v;
    }
    schema = prefixedSchema;
  }

  // apply strict option
  schema.$$strict = !options.strict as any;

  // create checker
  const check = validator.compile(schema);

fastest-validator

The fastest JS validator library for NodeJS

MIT
Latest version published 5 days ago

Package Health Score

78 / 100
Full package analysis

Popular fastest-validator functions