UNPKG

4.07 kBTypeScriptView Raw
1/** @module validate */
2import { IValidationRule } from './IValidationRule';
3import { ValidationResult } from './ValidationResult';
4import { Schema } from './Schema';
5import { PropertySchema } from './PropertySchema';
6/**
7 * Schema to validate user defined objects.
8 *
9 * ### Example ###
10 *
11 * let schema = new ObjectSchema(false)
12 * .withOptionalProperty("id", TypeCode.String)
13 * .withRequiredProperty("name", TypeCode.String);
14 *
15 * schema.validate({ id: "1", name: "ABC" }); // Result: no errors
16 * schema.validate({ name: "ABC" }); // Result: no errors
17 * schema.validate({ id: 1, name: "ABC" }); // Result: id type mismatch
18 * schema.validate({ id: 1, _name: "ABC" }); // Result: name is missing, unexpected _name
19 * schema.validate("ABC"); // Result: type mismatch
20 */
21export declare class ObjectSchema extends Schema {
22 private _properties;
23 private _allowUndefined;
24 /**
25 * Creates a new validation schema and sets its values.
26 *
27 * @param allowUndefined true to allow properties undefines in the schema
28 * @param required (optional) true to always require non-null values.
29 * @param rules (optional) a list with validation rules.
30 *
31 * @see [[IValidationRule]]
32 */
33 constructor(allowUndefined?: boolean, required?: boolean, rules?: IValidationRule[]);
34 /**
35 * Gets validation schemas for object properties.
36 *
37 * @returns the list of property validation schemas.
38 *
39 * @see [[PropertySchema]]
40 */
41 getProperties(): PropertySchema[];
42 /**
43 * Sets validation schemas for object properties.
44 *
45 * @param value a list of property validation schemas.
46 *
47 * @see [[PropertySchema]]
48 */
49 setProperties(value: PropertySchema[]): void;
50 /**
51 * Gets flag to allow undefined properties
52 *
53 * @returns true to allow undefined properties and false to disallow.
54 */
55 /**
56 * Sets flag to allow undefined properties
57 *
58 * @param value true to allow undefined properties and false to disallow.
59 */
60 isUndefinedAllowed: boolean;
61 /**
62 * Sets flag to allow undefined properties
63 *
64 * This method returns reference to this exception to implement Builder pattern
65 * to chain additional calls.
66 *
67 * @param value true to allow undefined properties and false to disallow.
68 * @returns this validation schema.
69 */
70 allowUndefined(value: boolean): ObjectSchema;
71 /**
72 * Adds a validation schema for an object property.
73 *
74 * This method returns reference to this exception to implement Builder pattern
75 * to chain additional calls.
76 *
77 * @param schema a property validation schema to be added.
78 * @returns this validation schema.
79 *
80 * @see [[PropertySchema]]
81 */
82 withProperty(schema: PropertySchema): ObjectSchema;
83 /**
84 * Adds a validation schema for a required object property.
85 *
86 * @param name a property name.
87 * @param type (optional) a property schema or type.
88 * @param rules (optional) a list of property validation rules.
89 */
90 withRequiredProperty(name: string, type?: any, ...rules: IValidationRule[]): ObjectSchema;
91 /**
92 * Adds a validation schema for an optional object property.
93 *
94 * @param name a property name.
95 * @param type (optional) a property schema or type.
96 * @param rules (optional) a list of property validation rules.
97 */
98 withOptionalProperty(name: string, type?: any, ...rules: IValidationRule[]): ObjectSchema;
99 /**
100 * Validates a given value against the schema and configured validation rules.
101 *
102 * @param path a dot notation path to the value.
103 * @param value a value to be validated.
104 * @param results a list with validation results to add new results.
105 */
106 protected performValidation(path: string, value: any, results: ValidationResult[]): void;
107}