0% found this document useful (0 votes)
6 views8 pages

Data Validation

The document discusses schema validation in MongoDB, which enforces data integrity by defining document structures using JSON Schema. It outlines various validation rules, levels, and practical implementation examples for different scenarios such as user registration, e-commerce, and employee management. The document emphasizes the importance of maintaining data consistency and preventing invalid data entry.

Uploaded by

khatoonsabiya159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Data Validation

The document discusses schema validation in MongoDB, which enforces data integrity by defining document structures using JSON Schema. It outlines various validation rules, levels, and practical implementation examples for different scenarios such as user registration, e-commerce, and employee management. The document emphasizes the importance of maintaining data consistency and preventing invalid data entry.

Uploaded by

khatoonsabiya159
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Data Validation: Theory

1. Schema Validation in MongoDB Schema validation in MongoDB is a mechanism to enforce data


integrity by defining a structure for documents within a collection. It uses the JSON Schema standard
to specify rules that documents must follow.

2. Validation Rules

• Required Fields: Ensure certain fields are always present.

• Data Types: Specify the data type for fields (e.g., string, number, date).

• Value Ranges: Enforce value constraints (e.g., minimum, maximum).

• Pattern Matching: Validate fields using regular expressions.

• Array Validations: Validate items in arrays for type, length, etc.

3. Validation Levels

• Strict: Reject documents that do not comply with the schema.

• Moderate: Accept documents without validation but warn during insertion.

4. Key Use Cases

• Data Integrity: Prevent invalid data from entering the database.

• Consistency: Enforce consistent structures across documents.

• Error Prevention: Identify issues early by validating user input.

Practical Implementation: Schema Validation in MongoDB

Setup Steps:

1. Use MongoDB Compass or the Mongo Shell.

2. Enable schema validation on a collection using $jsonSchema.

Example 1: User Registration

Scenario: Ensure every user document has username, email, and password. The email must be
unique and valid.

Schema:

json

Copy code

"$jsonSchema": {

"bsonType": "object",
"required": ["username", "email", "password"],

"properties": {

"username": {

"bsonType": "string",

"description": "must be a string and is required"

},

"email": {

"bsonType": "string",

"pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",

"description": "must be a valid email address and is required"

},

"password": {

"bsonType": "string",

"minLength": 8,

"description": "must be at least 8 characters long"

Create Collection:

javascript

Copy code

db.createCollection("users", {

validator: {

$jsonSchema: {

bsonType: "object",

required: ["username", "email", "password"],

properties: {

username: { bsonType: "string", description: "must be a string" },

email: {

bsonType: "string",
pattern: "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$",

description: "must be a valid email"

},

password: { bsonType: "string", minLength: 8, description: "min 8 chars" }

});

Example 2: E-commerce Product Catalog

Scenario: Ensure all products have a name, price, stock, and optional tags. Price must be positive,
and stock cannot be negative.

Schema:

json

Copy code

"$jsonSchema": {

"bsonType": "object",

"required": ["name", "price", "stock"],

"properties": {

"name": {

"bsonType": "string",

"description": "Product name is required"

},

"price": {

"bsonType": "double",

"minimum": 0,

"description": "Price must be a positive number"

},

"stock": {

"bsonType": "int",
"minimum": 0,

"description": "Stock cannot be negative"

},

"tags": {

"bsonType": "array",

"items": { "bsonType": "string" },

"description": "Optional tags"

Example 3: Employee Management

Scenario: Track employees with name, age, department, and hireDate. The age must be between 18
and 65, and hireDate must be a valid date.

Schema:

json

Copy code

"$jsonSchema": {

"bsonType": "object",

"required": ["name", "age", "department", "hireDate"],

"properties": {

"name": {

"bsonType": "string",

"description": "Name is required"

},

"age": {

"bsonType": "int",

"minimum": 18,

"maximum": 65,
"description": "Age must be between 18 and 65"

},

"department": {

"bsonType": "string",

"description": "Department is required"

},

"hireDate": {

"bsonType": "date",

"description": "Hire date must be a valid date"

Example 4: Customer Feedback

Scenario: Store feedback with a rating, comments, and submittedAt. Rating must be between 1 and
5.

Schema:

json

Copy code

"$jsonSchema": {

"bsonType": "object",

"required": ["rating", "comments", "submittedAt"],

"properties": {

"rating": {

"bsonType": "int",

"minimum": 1,

"maximum": 5,

"description": "Rating must be between 1 and 5"

},
"comments": {

"bsonType": "string",

"description": "Comments are required"

},

"submittedAt": {

"bsonType": "date",

"description": "Submission date is required"

Example 5: IoT Sensor Data

Scenario: Record sensor data with a sensorId, temperature, humidity, and timestamp. Temperature
must be within -50 to 150.

Schema:

json

Copy code

"$jsonSchema": {

"bsonType": "object",

"required": ["sensorId", "temperature", "humidity", "timestamp"],

"properties": {

"sensorId": {

"bsonType": "string",

"description": "Sensor ID is required"

},

"temperature": {

"bsonType": "double",

"minimum": -50,

"maximum": 150,
"description": "Temperature must be between -50 and 150"

},

"humidity": {

"bsonType": "double",

"minimum": 0,

"maximum": 100,

"description": "Humidity must be between 0 and 100"

},

"timestamp": {

"bsonType": "date",

"description": "Timestamp is required"

Validation in Action

To test validation:

1. Insert Valid Data:

javascript

Copy code

db.users.insertOne({

username: "john_doe",

email: "john@example.com",

password: "password123"

});

This will succeed.

2. Insert Invalid Data:

javascript

Copy code

db.users.insertOne({
username: "jane_doe",

email: "jane_at_example.com", // Invalid email

password: "123" // Too short

});

This will fail with a validation error.

Real-World Benefits

1. User Registration: Avoid duplicate accounts and enforce strong passwords.

2. E-commerce: Ensure products have valid prices and sufficient stock.

3. Employee Management: Maintain age and hire date consistency.

4. Feedback Analysis: Collect valid ratings for actionable insights.

5. IoT Systems: Prevent invalid sensor readings from corrupting analytics.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy