Socket
Socket
Sign inDemoInstall

simpl-schema

Package Overview
Dependencies
Maintainers
2
Versions
73
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simpl-schema - npm Package Compare versions

Comparing version 3.4.3 to 3.4.4

2

dist/cjs/doValidation.js

@@ -51,2 +51,3 @@ "use strict";

affectedKey: field.key,
keysToValidate,
obj,

@@ -66,2 +67,3 @@ op,

const fieldErrors = (0, validateField_js_1.default)({
keysToValidate,
obj,

@@ -68,0 +70,0 @@ schema,

216

dist/cjs/validation/validateField.js

@@ -92,123 +92,123 @@ "use strict";

validationContext, value: val }, (extendedCustomContext !== null && extendedCustomContext !== void 0 ? extendedCustomContext : {}));
if (!shouldValidateKey({
if (shouldValidateKey({
affectedKey,
affectedKeyGeneric: affectedKeyGeneric !== null && affectedKeyGeneric !== void 0 ? affectedKeyGeneric : undefined,
keysToValidate
}))
return [];
// Perform validation for this key
for (const currentDef of schema.getDefinitions(affectedKey, null, functionsContext)) {
def = currentDef;
// Whenever we try a new possible schema, clear any field errors from the previous tried schema
fieldValidationErrors.length = 0;
const validatorContext = Object.assign(Object.assign({}, functionsContext), { addValidationErrors(errors) {
errors.forEach((error) => fieldValidationErrors.push(error));
},
// Value checks are not necessary for null or undefined values, except
// for non-optional null array items, or for $unset or $rename values
valueShouldBeChecked: shouldCheckValue({
affectedKeyGeneric: affectedKeyGeneric !== null && affectedKeyGeneric !== void 0 ? affectedKeyGeneric : undefined,
isOptional: currentDef.optional,
op,
val
}) });
// Loop through each of the definitions in the SimpleSchemaGroup.
// If the value matches any, we are valid and can stop checking the rest.
for (const [typeIndex, typeDef] of currentDef.type.entries()) {
// If the type is SimpleSchema.Any, then it is valid
if (typeDef === SimpleSchema_js_1.SimpleSchema.Any)
break;
const nonAnyTypeDefinition = typeDef;
const { type } = currentDef, definitionWithoutType = __rest(currentDef
// @ts-expect-error
, ["type"]);
// @ts-expect-error
const finalValidatorContext = Object.assign(Object.assign({}, validatorContext), {
// Take outer definition props like "optional" and "label"
// and add them to inner props like "type" and "min"
definition: Object.assign(Object.assign({}, definitionWithoutType), nonAnyTypeDefinition) });
// Order of these validators is important
const customFieldValidator = nonAnyTypeDefinition.custom;
const fieldValidators = [
requiredValidator_js_1.default,
index_js_2.default,
allowedValuesValidator_js_1.default,
...(customFieldValidator == null ? [] : [customFieldValidator]),
// @ts-expect-error It's fine to access private method from here
...schema._validators,
// @ts-expect-error It's fine to access private method from here
...SimpleSchema_js_1.SimpleSchema._validators
];
const fieldValidationErrorsForThisType = [];
for (const fieldValidator of fieldValidators) {
const result = fieldValidator.call(finalValidatorContext);
// If the validator returns a string, assume it is the error type.
if (typeof result === 'string') {
fieldValidationErrorsForThisType.push({
name: affectedKey,
type: result,
value: val
})) {
// Perform validation for this key
for (const currentDef of schema.getDefinitions(affectedKey, null, functionsContext)) {
def = currentDef;
// Whenever we try a new possible schema, clear any field errors from the previous tried schema
fieldValidationErrors.length = 0;
const validatorContext = Object.assign(Object.assign({}, functionsContext), { addValidationErrors(errors) {
errors.forEach((error) => fieldValidationErrors.push(error));
},
// Value checks are not necessary for null or undefined values, except
// for non-optional null array items, or for $unset or $rename values
valueShouldBeChecked: shouldCheckValue({
affectedKeyGeneric: affectedKeyGeneric !== null && affectedKeyGeneric !== void 0 ? affectedKeyGeneric : undefined,
isOptional: currentDef.optional,
op,
val
}) });
// Loop through each of the definitions in the SimpleSchemaGroup.
// If the value matches any, we are valid and can stop checking the rest.
for (const [typeIndex, typeDef] of currentDef.type.entries()) {
// If the type is SimpleSchema.Any, then it is valid
if (typeDef === SimpleSchema_js_1.SimpleSchema.Any)
break;
const nonAnyTypeDefinition = typeDef;
const { type } = currentDef, definitionWithoutType = __rest(currentDef
// @ts-expect-error
, ["type"]);
// @ts-expect-error
const finalValidatorContext = Object.assign(Object.assign({}, validatorContext), {
// Take outer definition props like "optional" and "label"
// and add them to inner props like "type" and "min"
definition: Object.assign(Object.assign({}, definitionWithoutType), nonAnyTypeDefinition) });
// Order of these validators is important
const customFieldValidator = nonAnyTypeDefinition.custom;
const fieldValidators = [
requiredValidator_js_1.default,
index_js_2.default,
allowedValuesValidator_js_1.default,
...(customFieldValidator == null ? [] : [customFieldValidator]),
// @ts-expect-error It's fine to access private method from here
...schema._validators,
// @ts-expect-error It's fine to access private method from here
...SimpleSchema_js_1.SimpleSchema._validators
];
const fieldValidationErrorsForThisType = [];
for (const fieldValidator of fieldValidators) {
const result = fieldValidator.call(finalValidatorContext);
// If the validator returns a string, assume it is the error type.
if (typeof result === 'string') {
fieldValidationErrorsForThisType.push({
name: affectedKey,
type: result,
value: val
});
}
// If the validator returns an object, assume it is an error object.
if (typeof result === 'object' && result !== null) {
fieldValidationErrorsForThisType.push(Object.assign({ name: affectedKey, value: val }, result));
}
}
if (SimpleSchema_js_1.SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
const itemErrors = validateField({
extendedCustomContext,
keysToValidate,
obj: val,
op,
schema: nonAnyTypeDefinition.type,
val,
validationContext
});
if (itemErrors.length > 0) {
fieldValidationErrorsForThisType.push(...itemErrors.map((error) => (Object.assign(Object.assign({}, error), { name: `${affectedKey}.${error.name}` }))));
}
}
// If the validator returns an object, assume it is an error object.
if (typeof result === 'object' && result !== null) {
fieldValidationErrorsForThisType.push(Object.assign({ name: affectedKey, value: val }, result));
// As soon as we find a type for which the value is valid, stop checking more
if (fieldValidationErrorsForThisType.length === 0) {
// One we have chosen a valid schema, there is no need to validate the
// properties of this object because we validated all the way down
if (SimpleSchema_js_1.SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
return fieldValidationErrors;
}
break;
}
}
if (SimpleSchema_js_1.SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
const itemErrors = validateField({
extendedCustomContext,
keysToValidate,
obj: val,
op,
schema: nonAnyTypeDefinition.type,
val,
validationContext
});
if (itemErrors.length > 0) {
fieldValidationErrorsForThisType.push(...itemErrors.map((error) => (Object.assign(Object.assign({}, error), { name: `${affectedKey}.${error.name}` }))));
if (typeIndex === currentDef.type.length - 1) {
fieldValidationErrors.push(...fieldValidationErrorsForThisType);
}
}
// As soon as we find a type for which the value is valid, stop checking more
if (fieldValidationErrorsForThisType.length === 0) {
// One we have chosen a valid schema, there is no need to validate the
// properties of this object because we validated all the way down
if (SimpleSchema_js_1.SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
return fieldValidationErrors;
}
// If it's valid with this schema, we don't need to try any more
if (fieldValidationErrors.length === 0)
break;
}
// Mark invalid if not found in schema
if (def == null) {
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
if (op === '$unset' ||
(op === '$currentDate' && affectedKey.endsWith('.$type'))) {
return [];
}
if (typeIndex === currentDef.type.length - 1) {
fieldValidationErrors.push(...fieldValidationErrorsForThisType);
}
return [
{
name: affectedKey,
type: SimpleSchema_js_1.SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: val
}
];
}
// If it's valid with this schema, we don't need to try any more
if (fieldValidationErrors.length === 0)
break;
}
// Mark invalid if not found in schema
if (def == null) {
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
if (op === '$unset' ||
(op === '$currentDate' && affectedKey.endsWith('.$type'))) {
return [];
// For $rename, make sure that the new name is allowed by the schema
if (op === '$rename' && !schema.allowsKey(val)) {
return [
{
name: val,
type: SimpleSchema_js_1.SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: null
}
];
}
return [
{
name: affectedKey,
type: SimpleSchema_js_1.SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: val
}
];
}
// For $rename, make sure that the new name is allowed by the schema
if (op === '$rename' && !schema.allowsKey(val)) {
return [
{
name: val,
type: SimpleSchema_js_1.SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: null
}
];
}
// Loop through arrays

@@ -215,0 +215,0 @@ if (Array.isArray(val)) {

@@ -65,3 +65,3 @@ "use strict";

/**
* Validates the object against the SimpleSchema and sets a reactive array of error objects
* Validates the object against the SimpleSchema and sets an array of error objects
* @param obj Object to be validated

@@ -68,0 +68,0 @@ * @param options Validation options

@@ -46,2 +46,3 @@ import validateDocument from './validation/validateDocument.js';

affectedKey: field.key,
keysToValidate,
obj,

@@ -61,2 +62,3 @@ op,

const fieldErrors = validateField({
keysToValidate,
obj,

@@ -63,0 +65,0 @@ schema,

@@ -211,3 +211,3 @@ import { ClientError } from './errors.js';

/**
* Gets a field's label or all field labels reactively.
* Gets a field's label or all field labels
*

@@ -214,0 +214,0 @@ * @param key The schema key, specific or generic.

@@ -87,123 +87,123 @@ var __rest = (this && this.__rest) || function (s, e) {

validationContext, value: val }, (extendedCustomContext !== null && extendedCustomContext !== void 0 ? extendedCustomContext : {}));
if (!shouldValidateKey({
if (shouldValidateKey({
affectedKey,
affectedKeyGeneric: affectedKeyGeneric !== null && affectedKeyGeneric !== void 0 ? affectedKeyGeneric : undefined,
keysToValidate
}))
return [];
// Perform validation for this key
for (const currentDef of schema.getDefinitions(affectedKey, null, functionsContext)) {
def = currentDef;
// Whenever we try a new possible schema, clear any field errors from the previous tried schema
fieldValidationErrors.length = 0;
const validatorContext = Object.assign(Object.assign({}, functionsContext), { addValidationErrors(errors) {
errors.forEach((error) => fieldValidationErrors.push(error));
},
// Value checks are not necessary for null or undefined values, except
// for non-optional null array items, or for $unset or $rename values
valueShouldBeChecked: shouldCheckValue({
affectedKeyGeneric: affectedKeyGeneric !== null && affectedKeyGeneric !== void 0 ? affectedKeyGeneric : undefined,
isOptional: currentDef.optional,
op,
val
}) });
// Loop through each of the definitions in the SimpleSchemaGroup.
// If the value matches any, we are valid and can stop checking the rest.
for (const [typeIndex, typeDef] of currentDef.type.entries()) {
// If the type is SimpleSchema.Any, then it is valid
if (typeDef === SimpleSchema.Any)
break;
const nonAnyTypeDefinition = typeDef;
const { type } = currentDef, definitionWithoutType = __rest(currentDef
// @ts-expect-error
, ["type"]);
// @ts-expect-error
const finalValidatorContext = Object.assign(Object.assign({}, validatorContext), {
// Take outer definition props like "optional" and "label"
// and add them to inner props like "type" and "min"
definition: Object.assign(Object.assign({}, definitionWithoutType), nonAnyTypeDefinition) });
// Order of these validators is important
const customFieldValidator = nonAnyTypeDefinition.custom;
const fieldValidators = [
requiredValidator,
typeValidator,
allowedValuesValidator,
...(customFieldValidator == null ? [] : [customFieldValidator]),
// @ts-expect-error It's fine to access private method from here
...schema._validators,
// @ts-expect-error It's fine to access private method from here
...SimpleSchema._validators
];
const fieldValidationErrorsForThisType = [];
for (const fieldValidator of fieldValidators) {
const result = fieldValidator.call(finalValidatorContext);
// If the validator returns a string, assume it is the error type.
if (typeof result === 'string') {
fieldValidationErrorsForThisType.push({
name: affectedKey,
type: result,
value: val
})) {
// Perform validation for this key
for (const currentDef of schema.getDefinitions(affectedKey, null, functionsContext)) {
def = currentDef;
// Whenever we try a new possible schema, clear any field errors from the previous tried schema
fieldValidationErrors.length = 0;
const validatorContext = Object.assign(Object.assign({}, functionsContext), { addValidationErrors(errors) {
errors.forEach((error) => fieldValidationErrors.push(error));
},
// Value checks are not necessary for null or undefined values, except
// for non-optional null array items, or for $unset or $rename values
valueShouldBeChecked: shouldCheckValue({
affectedKeyGeneric: affectedKeyGeneric !== null && affectedKeyGeneric !== void 0 ? affectedKeyGeneric : undefined,
isOptional: currentDef.optional,
op,
val
}) });
// Loop through each of the definitions in the SimpleSchemaGroup.
// If the value matches any, we are valid and can stop checking the rest.
for (const [typeIndex, typeDef] of currentDef.type.entries()) {
// If the type is SimpleSchema.Any, then it is valid
if (typeDef === SimpleSchema.Any)
break;
const nonAnyTypeDefinition = typeDef;
const { type } = currentDef, definitionWithoutType = __rest(currentDef
// @ts-expect-error
, ["type"]);
// @ts-expect-error
const finalValidatorContext = Object.assign(Object.assign({}, validatorContext), {
// Take outer definition props like "optional" and "label"
// and add them to inner props like "type" and "min"
definition: Object.assign(Object.assign({}, definitionWithoutType), nonAnyTypeDefinition) });
// Order of these validators is important
const customFieldValidator = nonAnyTypeDefinition.custom;
const fieldValidators = [
requiredValidator,
typeValidator,
allowedValuesValidator,
...(customFieldValidator == null ? [] : [customFieldValidator]),
// @ts-expect-error It's fine to access private method from here
...schema._validators,
// @ts-expect-error It's fine to access private method from here
...SimpleSchema._validators
];
const fieldValidationErrorsForThisType = [];
for (const fieldValidator of fieldValidators) {
const result = fieldValidator.call(finalValidatorContext);
// If the validator returns a string, assume it is the error type.
if (typeof result === 'string') {
fieldValidationErrorsForThisType.push({
name: affectedKey,
type: result,
value: val
});
}
// If the validator returns an object, assume it is an error object.
if (typeof result === 'object' && result !== null) {
fieldValidationErrorsForThisType.push(Object.assign({ name: affectedKey, value: val }, result));
}
}
if (SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
const itemErrors = validateField({
extendedCustomContext,
keysToValidate,
obj: val,
op,
schema: nonAnyTypeDefinition.type,
val,
validationContext
});
if (itemErrors.length > 0) {
fieldValidationErrorsForThisType.push(...itemErrors.map((error) => (Object.assign(Object.assign({}, error), { name: `${affectedKey}.${error.name}` }))));
}
}
// If the validator returns an object, assume it is an error object.
if (typeof result === 'object' && result !== null) {
fieldValidationErrorsForThisType.push(Object.assign({ name: affectedKey, value: val }, result));
// As soon as we find a type for which the value is valid, stop checking more
if (fieldValidationErrorsForThisType.length === 0) {
// One we have chosen a valid schema, there is no need to validate the
// properties of this object because we validated all the way down
if (SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
return fieldValidationErrors;
}
break;
}
}
if (SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
const itemErrors = validateField({
extendedCustomContext,
keysToValidate,
obj: val,
op,
schema: nonAnyTypeDefinition.type,
val,
validationContext
});
if (itemErrors.length > 0) {
fieldValidationErrorsForThisType.push(...itemErrors.map((error) => (Object.assign(Object.assign({}, error), { name: `${affectedKey}.${error.name}` }))));
if (typeIndex === currentDef.type.length - 1) {
fieldValidationErrors.push(...fieldValidationErrorsForThisType);
}
}
// As soon as we find a type for which the value is valid, stop checking more
if (fieldValidationErrorsForThisType.length === 0) {
// One we have chosen a valid schema, there is no need to validate the
// properties of this object because we validated all the way down
if (SimpleSchema.isSimpleSchema(nonAnyTypeDefinition.type)) {
return fieldValidationErrors;
}
// If it's valid with this schema, we don't need to try any more
if (fieldValidationErrors.length === 0)
break;
}
// Mark invalid if not found in schema
if (def == null) {
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
if (op === '$unset' ||
(op === '$currentDate' && affectedKey.endsWith('.$type'))) {
return [];
}
if (typeIndex === currentDef.type.length - 1) {
fieldValidationErrors.push(...fieldValidationErrorsForThisType);
}
return [
{
name: affectedKey,
type: SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: val
}
];
}
// If it's valid with this schema, we don't need to try any more
if (fieldValidationErrors.length === 0)
break;
}
// Mark invalid if not found in schema
if (def == null) {
// We don't need KEY_NOT_IN_SCHEMA error for $unset and we also don't need to continue
if (op === '$unset' ||
(op === '$currentDate' && affectedKey.endsWith('.$type'))) {
return [];
// For $rename, make sure that the new name is allowed by the schema
if (op === '$rename' && !schema.allowsKey(val)) {
return [
{
name: val,
type: SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: null
}
];
}
return [
{
name: affectedKey,
type: SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: val
}
];
}
// For $rename, make sure that the new name is allowed by the schema
if (op === '$rename' && !schema.allowsKey(val)) {
return [
{
name: val,
type: SimpleSchema.ErrorTypes.KEY_NOT_IN_SCHEMA,
value: null
}
];
}
// Loop through arrays

@@ -210,0 +210,0 @@ if (Array.isArray(val)) {

@@ -39,3 +39,3 @@ import { SimpleSchema } from './SimpleSchema.js';

/**
* Validates the object against the SimpleSchema and sets a reactive array of error objects
* Validates the object against the SimpleSchema and sets an array of error objects
* @param obj Object to be validated

@@ -42,0 +42,0 @@ * @param options Validation options

@@ -60,3 +60,3 @@ import MongoObject from 'mongo-object';

/**
* Validates the object against the SimpleSchema and sets a reactive array of error objects
* Validates the object against the SimpleSchema and sets an array of error objects
* @param obj Object to be validated

@@ -63,0 +63,0 @@ * @param options Validation options

{
"name": "simpl-schema",
"version": "3.4.3",
"version": "3.4.4",
"description": "A schema validation package that supports direct validation of MongoDB update modifier objects.",

@@ -5,0 +5,0 @@ "author": "Eric Dobbertin <eric@dairystatedesigns.com>",

@@ -29,76 +29,77 @@ # SimpleSchema (simpl-schema NPM package)

- [Installation](#installation)
- [Lingo](#lingo)
- [The History of SimpleSchema](#the-history-of-simpleschema)
- [Quick Start](#quick-start)
- [Validate an Object and Throw an Error](#validate-an-object-and-throw-an-error)
- [Validate an Array of Objects and Throw an Error](#validate-an-array-of-objects-and-throw-an-error)
- [Validate an Object and Get the Errors](#validate-an-object-and-get-the-errors)
- [Validate a MongoDB Update Document](#validate-a-mongodb-update-document)
- [Automatically Clean the Object Before Validating It](#automatically-clean-the-object-before-validating-it)
- [Set Default Options for One Schema](#set-default-options-for-one-schema)
- [Set Default Options for All Schemas](#set-default-options-for-all-schemas)
- [Explicitly Clean an Object](#explicitly-clean-an-object)
- [Defining a Schema](#defining-a-schema)
- [Shorthand Definitions](#shorthand-definitions)
- [Longhand Definitions](#longhand-definitions)
- [Mixing Shorthand with Longhand](#mixing-shorthand-with-longhand)
- [More Shorthand](#more-shorthand)
- [Multiple Definitions For One Key](#multiple-definitions-for-one-key)
- [Extending Schemas](#extending-schemas)
- [Overriding When Extending](#overriding-when-extending)
- [Subschemas](#subschemas)
- [Extracting Schemas](#extracting-schemas)
- [Raw Definition](#raw-definition)
- [Schema Keys](#schema-keys)
- [Schema Rules](#schema-rules)
- [type](#type)
- [label](#label)
- [optional](#optional)
- [required](#required)
- [min/max](#minmax)
- [exclusiveMin/exclusiveMax](#exclusiveminexclusivemax)
- [minCount/maxCount](#mincountmaxcount)
- [allowedValues](#allowedvalues)
- [regEx](#regex)
- [skipRegExCheckForEmptyStrings](#skipregexcheckforemptystrings)
- [blackbox](#blackbox)
- [trim](#trim)
- [custom](#custom)
- [defaultValue](#defaultvalue)
- [autoValue](#autovalue)
- [autoValue gotchas](#autovalue-gotchas)
- [Function Properties](#function-properties)
- [Getting field properties](#getting-field-properties)
- [Validating Data](#validating-data)
- [The Object to Validate](#the-object-to-validate)
- [Ways to Perform Validation](#ways-to-perform-validation)
- [Named Validation Contexts](#named-validation-contexts)
- [Unnamed Validation Contexts](#unnamed-validation-contexts)
- [Validating an Object](#validating-an-object)
- [Validating Only Some Keys in an Object](#validating-only-some-keys-in-an-object)
- [Validation Options](#validation-options)
- [Validating and Throwing ValidationErrors](#validating-and-throwing-validationerrors)
- [Customize the Error That is Thrown](#customize-the-error-that-is-thrown)
- [Custom Field Validation](#custom-field-validation)
- [Custom Whole-Document Validators](#custom-whole-document-validators)
- [Manually Adding a Validation Error](#manually-adding-a-validation-error)
- [Getting a List of Invalid Keys and Validation Error Messages](#getting-a-list-of-invalid-keys-and-validation-error-messages)
- [Customizing Validation Messages](#customizing-validation-messages)
- [Other Validation Context Methods](#other-validation-context-methods)
- [Other SimpleSchema Methods](#other-simpleschema-methods)
- [Cleaning Objects](#cleaning-objects)
- [Dates](#dates)
- [Best Practice Code Examples](#best-practice-code-examples)
- [Make a field conditionally required](#make-a-field-conditionally-required)
- [Validate one key against another](#validate-one-key-against-another)
- [Debug Mode](#debug-mode)
- [Extending the Schema Options](#extending-the-schema-options)
- [Converting a SimpleSchema to a JSONSchema](#converting-a-simpleschema-to-a-jsonschema)
- [Add On Packages](#add-on-packages)
- [Contributors](#contributors)
- [Sponsors](#sponsors)
- [License](#license)
- [Contributing](#contributing)
- [Thanks](#thanks)
- [SimpleSchema (simpl-schema NPM package)](#simpleschema-simpl-schema-npm-package)
- [Installation](#installation)
- [Lingo](#lingo)
- [The History of SimpleSchema](#the-history-of-simpleschema)
- [Quick Start](#quick-start)
- [Validate an Object and Throw an Error](#validate-an-object-and-throw-an-error)
- [Validate an Array of Objects and Throw an Error](#validate-an-array-of-objects-and-throw-an-error)
- [Validate an Object and Get the Errors](#validate-an-object-and-get-the-errors)
- [Validate a MongoDB Update Document](#validate-a-mongodb-update-document)
- [Automatically Clean the Object Before Validating It](#automatically-clean-the-object-before-validating-it)
- [Set Default Options for One Schema](#set-default-options-for-one-schema)
- [Set Default Options for All Schemas](#set-default-options-for-all-schemas)
- [Explicitly Clean an Object](#explicitly-clean-an-object)
- [Defining a Schema](#defining-a-schema)
- [Shorthand Definitions](#shorthand-definitions)
- [Longhand Definitions](#longhand-definitions)
- [Mixing Shorthand with Longhand](#mixing-shorthand-with-longhand)
- [More Shorthand](#more-shorthand)
- [Multiple Definitions For One Key](#multiple-definitions-for-one-key)
- [Extending Schemas](#extending-schemas)
- [Overriding When Extending](#overriding-when-extending)
- [Subschemas](#subschemas)
- [Extracting Schemas](#extracting-schemas)
- [Raw Definition](#raw-definition)
- [Schema Keys](#schema-keys)
- [Schema Rules](#schema-rules)
- [type](#type)
- [label](#label)
- [optional](#optional)
- [required](#required)
- [min/max](#minmax)
- [exclusiveMin/exclusiveMax](#exclusiveminexclusivemax)
- [minCount/maxCount](#mincountmaxcount)
- [allowedValues](#allowedvalues)
- [regEx](#regex)
- [skipRegExCheckForEmptyStrings](#skipregexcheckforemptystrings)
- [blackbox](#blackbox)
- [trim](#trim)
- [custom](#custom)
- [defaultValue](#defaultvalue)
- [autoValue](#autovalue)
- [autoValue gotchas](#autovalue-gotchas)
- [Function Properties](#function-properties)
- [Getting field properties](#getting-field-properties)
- [Validating Data](#validating-data)
- [The Object to Validate](#the-object-to-validate)
- [Ways to Perform Validation](#ways-to-perform-validation)
- [Named Validation Contexts](#named-validation-contexts)
- [Unnamed Validation Contexts](#unnamed-validation-contexts)
- [Validating an Object](#validating-an-object)
- [Validating Only Some Keys in an Object](#validating-only-some-keys-in-an-object)
- [Validation Options](#validation-options)
- [Validating and Throwing ValidationErrors](#validating-and-throwing-validationerrors)
- [Customize the Error That is Thrown](#customize-the-error-that-is-thrown)
- [Custom Field Validation](#custom-field-validation)
- [Custom Whole-Document Validators](#custom-whole-document-validators)
- [Manually Adding a Validation Error](#manually-adding-a-validation-error)
- [Getting a List of Invalid Keys and Validation Error Messages](#getting-a-list-of-invalid-keys-and-validation-error-messages)
- [Customizing Validation Messages](#customizing-validation-messages)
- [Other Validation Context Methods](#other-validation-context-methods)
- [Other SimpleSchema Methods](#other-simpleschema-methods)
- [Cleaning Objects](#cleaning-objects)
- [Dates](#dates)
- [Best Practice Code Examples](#best-practice-code-examples)
- [Make a field conditionally required](#make-a-field-conditionally-required)
- [Validate one key against another](#validate-one-key-against-another)
- [Debug Mode](#debug-mode)
- [Extending the Schema Options](#extending-the-schema-options)
- [Converting a SimpleSchema to a JSONSchema](#converting-a-simpleschema-to-a-jsonschema)
- [Add On Packages](#add-on-packages)
- [Contributors](#contributors)
- [Sponsors](#sponsors)
- [License](#license)
- [Contributing](#contributing)
- [Thanks](#thanks)

@@ -860,7 +861,7 @@ <!-- END doctoc generated TOC please keep comment here to allow auto update -->

A validation context provides reactive methods for validating and checking the validation status of a particular object.
A validation context provides methods for validating and checking the validation status of a particular object.
#### Named Validation Contexts
It's usually best to use a named validation context. That way, the context is automatically persisted by name, allowing you to easily rely on its reactive methods.
It's usually best to use a named validation context. That way, the context is automatically persisted by name, allowing you to easily rely on its methods.

@@ -895,3 +896,3 @@ Here is an example of obtaining a named validation context:

An unnamed validation context is not persisted anywhere. It can be useful when you need to see if a document is valid but you don't need any of the reactive methods for that context, or if you are going to keep the context reference in memory yourself.
An unnamed validation context is not persisted anywhere. It can be useful when you need to see if a document is valid but you don't need any of the methods for that context, or if you are going to keep the context reference in memory yourself.

@@ -902,3 +903,3 @@ ### Validating an Object

You can call `myContext.isValid()` to see if the object last passed into `validate()` was found to be valid. This is a reactive method that returns `true` or `false`.
You can call `myContext.isValid()` to see if the object last passed into `validate()` was found to be valid. Returns `true` or `false`.

@@ -909,3 +910,3 @@ For a list of options, see the [Validation Options](#validation-options) section.

You may have the need to (re)validate certain keys while leaving any errors for other keys unchanged. For example, if you have several errors on a form and you want to revalidate only the invalid field the user is currently typing in. For this situation, call `myContext.validate` with the `keys` option set to an array of keys that should be validated. This may cause all of the reactive methods to react.
You may have the need to (re)validate certain keys while leaving any errors for other keys unchanged. For example, if you have several errors on a form and you want to revalidate only the invalid field the user is currently typing in. For this situation, call `myContext.validate` with the `keys` option set to an array of keys that should be validated.

@@ -1051,3 +1052,3 @@ This method returns `true` only if all the specified schema keys and their descendent keys are valid according to the schema. Otherwise it returns `false`.

If you want to reactively display an arbitrary validation error and it is not possible to use a custom validation function (perhaps you have to call a function `onSubmit` or wait for asynchronous results), you can add one or more errors to a validation context at any time by calling `myContext.addValidationErrors(errors)`, where `errors` is an array of error objects with the following format:
If you want to display an arbitrary validation error and it is not possible to use a custom validation function (perhaps you have to call a function `onSubmit` or wait for asynchronous results), you can add one or more errors to a validation context at any time by calling `myContext.addValidationErrors(errors)`, where `errors` is an array of error objects with the following format:

@@ -1074,4 +1075,2 @@ ```js

_This is a reactive method if you have enabled Tracker reactivity._
Call `myValidationContext.validationErrors()` to get the full array of validation errors. Each object in the array has at least two keys:

@@ -1117,7 +1116,6 @@

`myContext.keyIsInvalid(key)` returns true if the specified key is currently
invalid, or false if it is valid. This is a reactive method.
invalid, or false if it is valid.
`myContext.keyErrorMessage(key)` returns the error message for the specified
key if it is invalid. If it is valid, this method returns an empty string. This
is a reactive method.
key if it is invalid. If it is valid, this method returns an empty string.

@@ -1124,0 +1122,0 @@ Call `myContext.reset()` if you need to reset the validation context, clearing out any invalid field messages and making it valid.

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