konsumer / mongoose-type-url

A url field-type for Mongoose schemas
15 stars 6 forks source link

Fix #15 _checkRequired validation error #16

Closed grubshka closed 5 years ago

grubshka commented 5 years ago

This should fix the error.

konsumer commented 5 years ago

Why was this closed? Does it do what's intended?

grubshka commented 5 years ago

Not at all, it was a mistake. I had problems making the module properly, so I decided to do it simpler :

validateUrl.js :

import normalizeUrl from 'normalize-url';

var regUrl = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-/]))?/

export default function validateUrl(url) {
  if (!regUrl.test(url)) throw new Error(url + ' is not a valid URL');
  return normalizeUrl(url);
}

mySchema.js

const ProxySchema = new Schema(
    {
      url: {
        type: String,
        required: true,
        unique: true,
        validate: validateUrl
      },