Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema validation #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
245 changes: 98 additions & 147 deletions server/db.models.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import mongoose, { Schema } from 'mongoose';

const bcrypt = require('bcrypt');


import mongoose, { Schema } from "mongoose";
import yup from "yup";

// UNIQUE PROPERTY VALIDATOR
const mongooseUniqueValidator = require('mongoose-unique-validator');
const mongooseUniqueValidator = require("mongoose-unique-validator");

// SUPER ADMIN SCHEMA
const UserSchema = new Schema({
Expand Down Expand Up @@ -34,152 +31,107 @@ UserSchema.methods.comparePassword = (plaintext, callback) => { };


// SCHEMA BLUEPRINTS
const saccoSchema = new Schema({

name: {
type: String,
required: true,
},
address: {
type: String,
const saccoSchema = yup.object().shape(
{
name: yup.string().required(),
address: yup.string(),
// required: false
},
registration_number: {
type: String,
required: true,
unique: true,
},
contacts: {
email: {
type: String,
unique: true,
required: true,

},
telephone_number: {
type: String,
required: true,
unique: true,
},
},
membership: Number,
date_founded: {
type: Date,
required: true
},

about: {
description: String,
website: {

type: String,
validate: {
validator: link => link.indexOf('https://') === 0,
message: 'Webpage URL must start with https://',
},
registration_number: yup
.string()
.required()
.unique(),
contacts: yup
.email()
.string()
.required(),
telephone_number: yup
.string()
.required()
.unique(),
membership: yup.number(),
date_founded: yup.date().required(),
about: {
description: yup .string(),
website:yup
.string()
.test(link => link.indexOf("https://") === 0,
{message: "Webpage URL must start with https://"})
},
status: yup.string(),
created: yup
.date()
.default(() => (new Date()),
});

},
created: {
type: Date,
default: new Date(),
},
status: 'string',
username: { type: String, required: true, index: { unique: true } },
password: { type: String, required: true }
// ....

}, { strict: false });

// RIDER SCHEMA
const riderSchema = new Schema({
name: {
first_name: {
type: String,
required: true,
},
sur_name: {
type: String,
required: true,
const riderSchema = yup.object().shape(
{

first_name: yup
.string()
.required(),
sur_name: yup
.string()
.required(),
last_name: yup
.string()
.required(),
telephone_number: yup
.number()
.required(),
address: yup
.string()
.required(),
passport_photo:yup
.buffer(),
license_number:yup
.string()
.required(),
insurance: {
number: yup
.string()
// required: true,
.unique(),
issue_date: yup
.date()
.required(),
// default: new Date(),
exp_date: yup
.date()
.required()
// default: new Date(),
},
last_name: {
type: String,
required: true,
},
},
telephone_number: {
type: Number,
required: true,
},
address: {
type: String,
required: true,
},
passport_photo: {
type: Buffer,
required: false,
},
license_number: {
type: String,
required: true,
unique: true,
},
insurance: {
number: {
type: String,
// required: true,
unique: true,
},
issue_date: {
type: Date,
required: true,
// default: new Date(),
},
exp_date: {
type: Date,
required: true,
// default: new Date(),
},
},
// revisit
passport_ID: {
type: String,
required: true,
unique: true,

},
number_plate: {
type: String,
required: true,
unique: true,
validate: {
validator: text => text.indexOf('K') === 0,
message: 'Invalid number plate',
},

},
created: {
type: Date,
default: new Date(),
},
// react states
isActive: {
type: Boolean,
default: true,
},
// TODO challenge on how to implement ratings on the riders
ratings: [
{
numberOfStars: Number,
},
],
// THIS IS WHERE WE REFERENCE THE RIDER TO THEIR RESPECTIVE SACCOS
sacco: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Sacco',
},

}, { strict: false });
// revisit
passport_ID: yup
.string()
.required()
.unique(),
number_plate:yup
.string()
.required()
.unique()
.test(text => text.indexOf("K") === 0,
{message: "Invalid number plate"}),

created: yup
.date()
.default(() => (new Date()),
// react states
isActive:yup
.boolean()
.default('true'),
// TODO challenge on how to implement ratings on the riders
ratings: yup
.number(),
// THIS IS WHERE WE REFERENCE THE RIDER TO THEIR RESPECTIVE SACCOS
sacco: {
type: mongoose.Schema.Types.ObjectId,
ref: "Sacco"
}
},
{ strict: false }
);
// USING PLUGINS T
saccoSchema.plugin(mongooseUniqueValidator);
riderSchema.plugin(mongooseUniqueValidator);
Expand All @@ -192,7 +144,6 @@ const UserModel = mongoose.model("user", UserSchema);

// ++INSERTING SOME DATA INTO THE DATABASE++


module.exports = {
Sacco,
Rider,
Expand Down
2 changes: 1 addition & 1 deletion src/components/Registration.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { Component } from "react";
import SmsList from './SmsList'
import SmsList from './Smslist'



Expand Down