### Install via NPM Source: https://agustinsrg.github.io/javascript-object-sanitizer/index.html Use this command to add the library to an NPM-managed project. ```bash npm install @asanrom/javascript-object-sanitizer ``` -------------------------------- ### Import in Browser via Script Tag Source: https://agustinsrg.github.io/javascript-object-sanitizer/index.html Include the library in an HTML file by referencing the minified file path. ```html ``` -------------------------------- ### AbstractObjectSchema Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/AbstractObjectSchema.html Documentation for the core methods available on the AbstractObjectSchema class. ```APIDOC ## sanitize(object: any) ### Description Sanitizes an object based on the defined schema. ### Parameters - **object** (any) - Required - The object to sanitize. ### Response - **Returns** (any) - The sanitized object. --- ## test(object: any) ### Description Tests the schema against an object to verify if it matches. ### Parameters - **object** (any) - Required - The object to test. ### Response - **Returns** (boolean) - true if it matches the schema, false if it does not match. --- ## provideTestError(object: any) ### Description Provides the reason why the test() method returned false. ### Parameters - **object** (any) - Required - The object to test. ### Response - **Returns** (Error) - The provided error detailing the mismatch. --- ## getRawSchema() ### Description Gets the raw schema as an object type. ### Response - **Returns** (RawObjectSchema) - The raw schema definition. ``` -------------------------------- ### Define and Use Schema in Browser Source: https://agustinsrg.github.io/javascript-object-sanitizer/index.html Access the ObjectSchema from the global window object after loading the library script. ```javascript window.ObjectSchema = ObjectSanitizer.ObjectSchema; // Example schema or complex object const schema = ObjectSchema.object({ stringProperty: ObjectSchema.string().withDefaultValue(""), integerProperty: ObjectSchema.integer(), positiveNumber: ObjectSchema.number().withMin(0), array: ObjectSchema.array(ObjectSchema.object({ arrayItemProp1: ObjectSchema.boolean(), })), optionalProperty: ObjectSchema.optional(ObjectSchema.number()), }); // Test user input schema.test(userInput); // Returns true or false // Sanitize const sanitized = schema.sanitize(userInput); // Forces user input to follow the schema ``` -------------------------------- ### ObjectSchema Class Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/ObjectSchema.html Documentation for the ObjectSchema class, including its constructor, properties, and methods for defining and manipulating object schemas. ```APIDOC ## Class ObjectSchema Object schema ### Hierarchy * AbstractObjectSchema * ObjectSchema ## Constructors ### constructor * new ObjectSchema(): ObjectSchema * Constructor #### Returns ObjectSchema ## Properties ### Private schema schema: ObjectRawSchema ## Methods ### getRawSchema * getRawSchema(): RawObjectSchema * Gets the raw schema, as object type #### Returns RawObjectSchema ### provideTestError * provideTestError(object: any): Error * Provides the reason test() returned false, if any #### Parameters * ##### object: any The object to test #### Returns Error The provided error ### sanitize * sanitize(object: any): any * Sanitizes an object. #### Parameters * ##### object: any The object to test #### Returns any The sanitized object ### test * test(object: any): boolean * Tests the schema against an object #### Parameters * ##### object: any The object to test #### Returns boolean true if it matches the schema, false if it does not match ### withId * withId(id: string): ObjectSchema * Sets the ID for this schema to be referenced by its children for recursion #### Parameters * ##### id: string The identifier of the schema node #### Returns ObjectSchema self ### withProperties * withProperties(props: {}): ObjectSchema * Sets required properties for the object #### Parameters * ##### props: {} Properties * ##### [propName: string]: ObjectSchemaI #### Returns ObjectSchema self ### withProperty * withProperty(propName: string, schema: ObjectSchemaI): ObjectSchema * Add property to the object schema #### Parameters * ##### propName: string The name of the property * ##### schema: ObjectSchemaI The schema for the property #### Returns ObjectSchema self ### withPropertyFilter * withPropertyFilter(extraPropsFilter: (key: string) => boolean, extraPropsSchema: (key: string) => ObjectSchemaI): ObjectSchema * Sets property filter for dynamic dictionaries #### Parameters * ##### extraPropsFilter: (key: string) => boolean Function to filter the the propeties * * (key: string): boolean * #### Parameters * ##### key: string #### Returns boolean * ##### extraPropsSchema: (key: string) => ObjectSchemaI Function to determine the schema to use in each case * * (key: string): ObjectSchemaI * #### Parameters * ##### key: string #### Returns ObjectSchemaI #### Returns ObjectSchema self ``` -------------------------------- ### Static Schema Creation Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/ObjectSchema.html Static methods available on ObjectSchema for creating various types of schemas. ```APIDOC ### Static anyOf * anyOf(schemas: ObjectSchemaI[]): AnyOfObjectSchema * The object should match one of the provided schemas #### Parameters * ##### schemas: ObjectSchemaI[] List of schemas #### Returns AnyOfObjectSchema a new schema instance ### Static array * array(itemsSchema: ObjectSchemaI): ArrayObjectSchema * Schema to represent an array #### Parameters * ##### itemsSchema: ObjectSchemaI Schema for the items of the array #### Returns ArrayObjectSchema a new schema instance ### Static boolean * boolean(): BooleanObjectSchema * Schema to represent a boolean #### Returns BooleanObjectSchema a new schema instance ### Static create * create(): ObjectSchema * Schema for object type #### Returns ObjectSchema a new schema instance ### Static custom * custom(): CustomObjectSchema * Schema with custom tester and sanitizer #### Returns CustomObjectSchema a new schema instance ### Static dict * dict(propsFilter: (key: string) => boolean, propsSchema: (key: string) => ObjectSchemaI): ObjectSchema * Schema for dictionary (key => value) #### Parameters * ##### propsFilter: (key: string) => boolean Property filter * * (key: string): boolean * #### Parameters * ##### key: string #### Returns boolean * ##### propsSchema: (key: string) => ObjectSchemaI Function to determine schema for properties * * (key: string): ObjectSchemaI * #### Parameters * ##### key: string #### Returns ObjectSchemaI #### Returns ObjectSchema a new schema instance ### Static extend * extend(schema: ObjectSchemaI, ext: {}): ObjectSchemaI * Extends schema. For object schemas with pre-defined keys ``` -------------------------------- ### Classes Source: https://agustinsrg.github.io/javascript-object-sanitizer/modules.html Lists the available classes for defining and sanitizing object schemas. ```APIDOC ## Classes * AbstractObjectSchema * AnyOfObjectSchema * ArrayObjectSchema * BooleanObjectSchema * CustomObjectSchema * NumberObjectSchema * ObjectSchema * RecursiveObjectSchema * StringObjectSchema ``` -------------------------------- ### Define and Use Schema in Node.js Source: https://agustinsrg.github.io/javascript-object-sanitizer/index.html Import the ObjectSchema class to define validation rules and sanitize input objects. ```javascript import { ObjectSchema } from '@asanrom/javascript-object-sanitizer'; // With require: const ObjectSchema = require('@asanrom/javascript-object-sanitizer').ObjectSchema; // Example schema or complex object const schema = ObjectSchema.object({ stringProperty: ObjectSchema.string().withDefaultValue(""), integerProperty: ObjectSchema.integer(), positiveNumber: ObjectSchema.number().withMin(0), array: ObjectSchema.array(ObjectSchema.object({ arrayItemProp1: ObjectSchema.boolean(), })), optionalProperty: ObjectSchema.optional(ObjectSchema.number()), }); // Test user input schema.test(userInput); // Returns true or false // Sanitize const sanitized = schema.sanitize(userInput); // Forces user input to follow the schema ``` -------------------------------- ### AnyOfObjectSchema Class Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/AnyOfObjectSchema.html Methods for interacting with the AnyOfObjectSchema class to sanitize and test objects. ```APIDOC ## sanitize(object) ### Description Sanitizes an object based on the defined schema options. ### Parameters - **object** (any) - Required - The object to sanitize. ### Response - **Returns** (any) - The sanitized object. ## test(object) ### Description Tests the schema against an object to determine if it matches. ### Parameters - **object** (any) - Required - The object to test. ### Response - **Returns** (boolean) - true if it matches the schema, false otherwise. ## provideTestError(object) ### Description Provides the reason why the test() method returned false. ### Parameters - **object** (any) - Required - The object to test. ### Response - **Returns** (Error) - The error describing the validation failure. ``` -------------------------------- ### Interfaces Source: https://agustinsrg.github.io/javascript-object-sanitizer/modules.html Defines the interface for object schemas. ```APIDOC ## Interfaces * ObjectSchemaI ``` -------------------------------- ### RecursiveObjectSchema Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/RecursiveObjectSchema.html API documentation for the methods available in the RecursiveObjectSchema class. ```APIDOC ## sanitize(object: any) ### Description Sanitizes an object based on the defined schema. ### Parameters - **object** (any) - Required - The object to sanitize. ### Response - **Returns** (any) - The sanitized object. ## test(object: any) ### Description Tests the schema against an object. ### Parameters - **object** (any) - Required - The object to test. ### Response - **Returns** (boolean) - true if it matches the schema, false otherwise. ## withLevelsUp(up: number) ### Description Set number of levels up to raise in the schema. Defaults to 1. ### Parameters - **up** (number) - Required - Levels to raise in the schema. ### Response - **Returns** (RecursiveObjectSchema) - self ## withMaxRecursion(maxRecursion: number) ### Description Sets max recursion allowed. Default has no limit. ### Parameters - **maxRecursion** (number) - Required - Max levels of recursion allowed. ### Response - **Returns** (RecursiveObjectSchema) - self ## withReference(ref: string) ### Description Sets the recursive reference, based on parent id. ### Parameters - **ref** (string) - Required - The Node id to reference. ### Response - **Returns** (RecursiveObjectSchema) - self ``` -------------------------------- ### Schema Types and Static Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/ObjectSchema.html This section details the various schema types and static methods provided by the JavaScript Object Sanitizer library. ```APIDOC ## Schema Types and Static Methods This section details the various schema types and static methods provided by the JavaScript Object Sanitizer library. ### Static string * string(): StringObjectSchema * Schema to represent a string #### Returns StringObjectSchema a new schema instance ### Static integer * integer(): NumberObjectSchema * Schema to represent an integer #### Returns NumberObjectSchema a new schema instance ### Static null * null(): ObjectSchemaI * Schema to represent the null value #### Returns ObjectSchemaI a new schema instance ### Static number * number(): NumberObjectSchema * Schema to represent a real number #### Returns NumberObjectSchema a new schema instance ### Static object * object(props: {}): ObjectSchema * Schema for object type (with a set of properties) #### Parameters * ##### props: {} Properties * ##### [propName: string]: ObjectSchemaI #### Returns ObjectSchema a new schema instance ### Static optional * optional(schema: ObjectSchemaI): AnyOfObjectSchema * Makes schema optional. This means undefined is allowed for test() #### Parameters * ##### schema: ObjectSchemaI The schema #### Returns AnyOfObjectSchema a new schema instance ### Static parent * parent(): RecursiveObjectSchema * Parent recursive reference #### Returns RecursiveObjectSchema a new schema instance ### Static recursive * recursive(): RecursiveObjectSchema * Schema for recursive references #### Returns RecursiveObjectSchema a new schema instance ### Static undefined * undefined(): ObjectSchemaI * Schema to represent the undefined value #### Returns ObjectSchemaI a new schema instance ### Static fromRaw * fromRaw(raw: ObjectRawSchema): ObjectSchemaI * Schema from raw representation #### Parameters * ##### raw: ObjectRawSchema Raw schema representation #### Returns ObjectSchemaI a new schema instance ``` -------------------------------- ### CustomObjectSchema Class Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/CustomObjectSchema.html Documentation for the CustomObjectSchema class, which allows for custom sanitization and testing logic. ```APIDOC ## Class CustomObjectSchema Custom sanitizer schema ### Hierarchy * AbstractObjectSchema * CustomObjectSchema ## Constructors ### constructor * new CustomObjectSchema(test: (object: any) => boolean, sanitize: (object: any) => any): CustomObjectSchema * Constructor #### Parameters * ##### test: (object: any) => boolean Schema tester * * (object: any): boolean * #### Parameters * ##### object: any #### Returns boolean * ##### sanitize: (object: any) => any Schema sanitizer * * (object: any): any * #### Parameters * ##### object: any #### Returns any #### Returns CustomObjectSchema ## Properties ### Private schema schema: CustomSanitizeRawSchema ## Methods ### getRawSchema * getRawSchema(): RawObjectSchema * Gets the raw schema, as object type #### Returns RawObjectSchema ### provideTestError * provideTestError(object: any): Error * Provides the reason test() returned false, if any #### Parameters * ##### object: any The object to test #### Returns Error The provided error ### sanitize * sanitize(object: any): any * Sanitizes an object. #### Parameters * ##### object: any The object to test #### Returns any The sanitized object ### test * test(object: any): boolean * Tests the schema against an object #### Parameters * ##### object: any The object to test #### Returns boolean true if it matches the schema, false if it does not match ### withSanitizer * withSanitizer(sanitize: (object: any) => any): CustomObjectSchema * Sets custom sanitizer #### Parameters * ##### sanitize: (object: any) => any Schema sanitizer * * (object: any): any * #### Parameters * ##### object: any #### Returns any #### Returns CustomObjectSchema self ### withTester * withTester(test: (object: any) => boolean): CustomObjectSchema * Sets custom tester #### Parameters * ##### test: (object: any) => boolean Schema tester * * (object: any): boolean * #### Parameters * ##### object: any #### Returns boolean #### Returns CustomObjectSchema self ### Static create * create(): CustomObjectSchema * Creates a custom object schema. You can set custom tester and sanitizer functions. #### Returns CustomObjectSchema a new instance of CustomObjectSchema ``` -------------------------------- ### ObjectSchemaI Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/interfaces/ObjectSchemaI.html Methods available for the ObjectSchemaI interface to handle object validation and sanitization. ```APIDOC ## getRawSchema ### Description Gets the raw schema as an object type. ### Returns - **RawObjectSchema** - The raw schema definition. ## provideTestError ### Description Provides the reason the test() method returned false, if any. ### Parameters - **object** (any) - Required - The object to test. ### Returns - **Error** - The provided error object. ## sanitize ### Description Sanitizes an object based on the schema. ### Parameters - **object** (any) - Required - The object to sanitize. ### Returns - **any** - The sanitized object. ## test ### Description Tests the schema against an object. ### Parameters - **object** (any) - Required - The object to test. ### Returns - **boolean** - true if it matches the schema, false if it does not match. ``` -------------------------------- ### BooleanObjectSchema Class Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/BooleanObjectSchema.html Documentation for the BooleanObjectSchema class, used for defining schemas for boolean values. ```APIDOC ## Class BooleanObjectSchema Schema for boolean ### Hierarchy * AbstractObjectSchema * BooleanObjectSchema ## Index ### Constructors * constructor ### Properties * schema ### Methods * getRawSchema * provideTestError * sanitize * test * withDefaultValue * withEnumeration * create ## Constructors ### constructor * new BooleanObjectSchema(): BooleanObjectSchema * Constructor #### Returns BooleanObjectSchema ## Properties ### Private schema schema: BooleanRawSchema ## Methods ### getRawSchema * getRawSchema(): RawObjectSchema * Gets the raw schema, as object type #### Returns RawObjectSchema ### provideTestError * provideTestError(object: any): Error * Provides the reason test() returned false, if any #### Parameters * ##### object: any The object to test #### Returns Error The provided error ### sanitize * sanitize(object: any): any * Sanitizes an object. #### Parameters * ##### object: any The object to test #### Returns any The sanitized object ### test * test(object: any): boolean * Tests the schema against an object #### Parameters * ##### object: any The object to test #### Returns boolean true if it matches the schema, false if it does not match ### withDefaultValue * withDefaultValue(defaultValue: boolean): BooleanObjectSchema * Sets default value, in case the object is null, undefined or cannot be parsed #### Parameters * ##### defaultValue: boolean The default value #### Returns BooleanObjectSchema self ### withEnumeration * withEnumeration(enumeration: boolean[]): BooleanObjectSchema * Sets an enumeration of allowed values #### Parameters * ##### enumeration: boolean[] The enumeration #### Returns BooleanObjectSchema self ### Static create * create(): BooleanObjectSchema * Creates boolean schema #### Returns BooleanObjectSchema a new instance of BooleanObjectSchema ``` -------------------------------- ### Type Aliases Source: https://agustinsrg.github.io/javascript-object-sanitizer/modules.html Defines the possible structures for raw object schemas used in sanitization. ```APIDOC ## Type aliases ### RawObjectSchema `RawObjectSchema`: `CustomSanitizeRawSchema` | `NullOrUndefinedRawSchema` | `BooleanRawSchema` | `StringRawSchema` | `NumberRawSchema` | `ArrayRawSchema` | `ObjectRawSchema` | `RecursiveRawSchema` | `AnyOfRawSchema` ``` -------------------------------- ### StringObjectSchema Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/StringObjectSchema.html Methods available for the StringObjectSchema class to configure and execute string validation and sanitization. ```APIDOC ## StringObjectSchema Methods ### sanitize - **Description**: Sanitizes an object based on the defined schema. - **Parameters**: - **object** (any) - The object to sanitize. - **Returns**: (any) The sanitized object. ### test - **Description**: Tests the schema against an object. - **Parameters**: - **object** (any) - The object to test. - **Returns**: (boolean) true if it matches the schema, false otherwise. ### withDefaultValue - **Description**: Sets a default value if the object is null, undefined, or cannot be parsed. - **Parameters**: - **defaultValue** (string) - The default value. ### withEnumeration - **Description**: Sets an enumeration of allowed values. - **Parameters**: - **enumeration** (string[]) - The allowed values. ### withMaxLength - **Description**: Sets the maximum length for the string. Truncates if exceeded. - **Parameters**: - **maxLength** (number) - The max length. ### withRegularExpression - **Description**: Sets a regular expression the string must match. - **Parameters**: - **regExp** (RegExp) - The regular expression. ``` -------------------------------- ### NumberObjectSchema Class Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/NumberObjectSchema.html Provides methods for creating and configuring a schema to sanitize number values. ```APIDOC ## Class NumberObjectSchema Schema for Number ### Hierarchy * AbstractObjectSchema * NumberObjectSchema ## Constructors ### constructor * new NumberObjectSchema(): NumberObjectSchema * Constructor #### Returns NumberObjectSchema self ## Properties ### Private schema schema: NumberRawSchema ## Methods ### allowInfinite * allowInfinite(): NumberObjectSchema * Allos Infinite values By default, infinite values are ignored, and default value is set. #### Returns NumberObjectSchema self ### allowNaN * allowNaN(): NumberObjectSchema * Allows NaN as an option. By default, if NaN, the default value is set. #### Returns NumberObjectSchema self ### forceInteger * forceInteger(): NumberObjectSchema * Forces number to integer #### Returns NumberObjectSchema self ### getRawSchema * getRawSchema(): RawObjectSchema * Gets the raw schema, as object type #### Returns RawObjectSchema RawObjectSchema ### provideTestError * provideTestError(object: any): Error * Provides the reason test() returned false, if any #### Parameters * ##### object: any The object to test #### Returns Error The provided error ### sanitize * sanitize(object: any): any * Sanitizes an object. #### Parameters * ##### object: any The object to test #### Returns any The sanitized object ### test * test(object: any): boolean * Tests the schema against an object #### Parameters * ##### object: any The object to test #### Returns boolean true if it matches the schema, false if it does not match ### withDefaultValue * withDefaultValue(defaultValue: number): NumberObjectSchema * Sets default value, in case the object is null, undefined or cannot be parsed #### Parameters * ##### defaultValue: number The default value #### Returns NumberObjectSchema self ### withEnumeration * withEnumeration(enumeration: number[]): NumberObjectSchema * Sets an enumeration of allowed values #### Parameters * ##### enumeration: number[] The enumeration #### Returns NumberObjectSchema self ### withMax * withMax(max: number): NumberObjectSchema * Sets the max value. if the value is greater, this value will be set. #### Parameters * ##### max: number The max value #### Returns NumberObjectSchema self ### withMin * withMin(min: number): NumberObjectSchema * Sets the min value. If the value is lower, this value will be set. #### Parameters * ##### min: number The min value #### Returns NumberObjectSchema self ### Static create * create(): NumberObjectSchema * Creates number schema #### Returns NumberObjectSchema a new instance of NumberObjectSchema ``` -------------------------------- ### ArrayObjectSchema Methods Source: https://agustinsrg.github.io/javascript-object-sanitizer/classes/ArrayObjectSchema.html Methods available for the ArrayObjectSchema class to handle array validation and sanitization. ```APIDOC ## constructor ### Description Creates a new instance of ArrayObjectSchema. ### Parameters - **itemsSchema** (ObjectSchemaI) - Required - Schema for the array items. ## sanitize ### Description Sanitizes an object based on the defined array schema. ### Parameters - **object** (any) - Required - The object to sanitize. ### Response - **any** - The sanitized object. ## test ### Description Tests the schema against an object. ### Parameters - **object** (any) - Required - The object to test. ### Response - **boolean** - true if it matches the schema, false otherwise. ## withMaxLength ### Description Sets the maximum length for the array. If the array length is greater than this value, it will be truncated. ### Parameters - **maxLength** (number) - Required - The max length for the array. ### Response - **ArrayObjectSchema** - Returns self. ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.