Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'fastest-validator' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
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)
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) {
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 });
}
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);