예제 #1
0
// Validate does some validation to be able to store the record.
func (u *Registry) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Name, "1", "255") {
		db.AddError(fmt.Errorf("Name should be longer than 1 and shorter than 255"))
	}

	if u.Name != "" {
		notFound := db.Where(
			"name = ?",
			u.Name,
		).Not(
			"id",
			u.ID,
		).First(
			&Registry{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Name is already present"))
		}
	}

	if !govalidator.StringLength(u.Host, "1", "255") {
		db.AddError(fmt.Errorf("Host should be longer than 1 and shorter than 255"))
	}

	if !govalidator.IsRequestURL(u.Host) {
		db.AddError(fmt.Errorf(
			"Host must be a valid URL",
		))
	}

	if u.Host != "" {
		notFound := db.Where(
			"host = ?",
			u.Host,
		).Not(
			"id",
			u.ID,
		).First(
			&Registry{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Host is already present"))
		}
	}
}
예제 #2
0
func (c Validator) IsModelValid(climb interface{}, ctx validation.Context) error {
	m := climb.(Model)
	e := boom.NewError("Validation error. Please fix the errors before continuing.")

	if b := govalidator.StringLength(m.Name, "0", "15"); !b {
		e.AddError("Name", "cannot exceed 15 characters")
	}

	if b := govalidator.InRange(float64(m.Rating), 0, 16); !b {
		e.AddError("Rating", fmt.Sprintf("%v is outside range [0, 16]"))
	}

	if b := govalidator.StringLength(m.Description, "0", "255"); !b {
		e.AddError("Description", fmt.Sprintf("cannot exceed 255 characters"))
	}

	if len(e.Errors) == 0 {
		return nil
	} else {
		return e
	}
}
예제 #3
0
// Validate does some validation to be able to store the record.
func (u *Team) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Name, "1", "255") {
		db.AddError(fmt.Errorf("Name should be longer than 1 and shorter than 255"))
	}

	if u.Name != "" {
		notFound := db.Where(
			"name = ?",
			u.Name,
		).Not(
			"id",
			u.ID,
		).First(
			&Team{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Name is already present"))
		}
	}
}
예제 #4
0
// Validate does some validation to be able to store the record.
func (u *User) Validate(db *gorm.DB) {
	if !govalidator.StringLength(u.Username, "2", "255") {
		db.AddError(fmt.Errorf("Username should be longer than 2 and shorter than 255"))
	}

	if u.Username != "" {
		notFound := db.Where(
			"username = ?",
			u.Username,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Username is already present"))
		}
	}

	if u.Hash != "" {
		notFound := db.Where(
			"hash = ?",
			u.Hash,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Hash is already present"))
		}
	}

	if !govalidator.IsEmail(u.Email) {
		db.AddError(fmt.Errorf(
			"Email must be a valid email address",
		))
	}

	if u.Email != "" {
		normalized, _ := govalidator.NormalizeEmail(
			u.Email,
		)

		notFound := db.Where(
			"email = ?",
			normalized,
		).Not(
			"id",
			u.ID,
		).First(
			&User{},
		).RecordNotFound()

		if !notFound {
			db.AddError(fmt.Errorf("Email is already present"))
		}
	}

	if db.NewRecord(u) {
		if !govalidator.StringLength(u.Password, "5", "255") {
			db.AddError(fmt.Errorf("Password should be longer than 5 and shorter than 255"))
		}
	}
}
예제 #5
0
파일: service.go 프로젝트: ownode/ownode
// create a service
func (c *ServiceController) Create(res http.ResponseWriter, req services.AuxRequestContext, db *services.DB) {

	// parse request body
	var body createBody
	if err := c.ParseJsonBody(req, &body); err != nil {
		services.Res(res).Error(400, "invalid_client", "request body is invalid or malformed. Expects valid json body")
		return
	}

	// name is required
	if c.validate.IsEmpty(body.FullName) {
		services.Res(res).Error(400, "missing_parameter", "Missing required field: full_name")
		return
	}

	// full name must have max of 60 characters
	if !validator.StringLength(body.FullName, "1", "60") {
		services.Res(res).Error(400, "invalid_full_name", "full_name character limit is 60")
		return
	}

	// service name is required
	if c.validate.IsEmpty(body.ServiceName) {
		services.Res(res).Error(400, "missing_parameter", "Missing required field: service_name")
		return
	}

	// service name must have max of 30 characters
	if !validator.StringLength(body.ServiceName, "1", "60") {
		services.Res(res).Error(400, "invalid_service_name", "service_name character limit is 60")
		return
	}

	// description is required
	if c.validate.IsEmpty(body.Description) {
		services.Res(res).Error(400, "missing_parameter", "Missing required field: description")
		return
	}

	// description must have max of 140 characters
	if !validator.StringLength(body.Description, "1", "140") {
		services.Res(res).Error(400, "invalid_description", "description character limit is 140")
		return
	}

	// email is required
	if c.validate.IsEmpty(body.Email) {
		services.Res(res).Error(400, "missing_parameter", "Missing required field: email")
		return
	}

	// email must be valid
	if !validator.IsEmail(body.Email) {
		services.Res(res).Error(400, "invalid_email", "email is invalid")
		return
	}

	// create identity
	newIdentity := &models.Identity{
		ObjectID: bson.NewObjectId().Hex(),
		FullName: body.FullName,
		Email:    body.Email,
	}

	// start db transaction
	dbTx := db.GetPostgresHandle().Begin()

	if err := models.CreateIdentity(dbTx, newIdentity); err != nil {
		dbTx.Rollback()
		c.log.Error(err.Error())
		services.Res(res).Error(500, "", "server error")
		return
	}

	// create client credentials
	clientId := services.GetRandString(services.GetRandNumRange(32, 42))
	clientSecret := services.GetRandString(services.GetRandNumRange(32, 42))

	// create new service object
	newService := models.Service{
		ObjectID:     bson.NewObjectId().Hex(),
		Name:         body.ServiceName,
		Description:  body.Description,
		ClientID:     clientId,
		ClientSecret: clientSecret,
		Identity:     newIdentity,
	}

	// create service
	err := models.CreateService(dbTx, &newService)
	if err != nil {
		dbTx.Rollback()
		c.log.Error(err.Error())
		services.Res(res).Error(500, "", "server error")
		return
	}

	// commit db transaction
	dbTx.Commit()

	// send response
	respObj, _ := services.StructToJsonToMap(newService)
	services.Res(res).Json(respObj)
}