Code Monkey home page Code Monkey logo

alamid-schema's People

Contributors

benurb avatar jhnns avatar meaku avatar sbat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

alamid-schema's Issues

Should validators evaluate if an attribute is not required and undefined?

Consider the following example:

var PandaSchema = new Schema({
    name: {
        type: String,
        required: true
    },
    age: {
        type: Number,
        min: 9,
        max: 99
    },
    mood: {
        type: String,
        enum: ["happy", "sleepy"]
    }
});

var panda = {
    name: "Hugo"
};

The attributes age and mood are not required, but other validators will evaluate anyway. Is this wanted behaviour? Imo this should not be the case.

As we like mongoose, this is was mongoose says:

Validators are not run on undefined values. The only exception is the required validator.

Lousy documentation

How am I to save this schema" Mongoose/Mongo won't do it; you do not show how.

Lousy documentation.

replace lib/merge with a module

Wouldn't it be much nicer to use a module for merge.js instead of self-maintain this functionality?

A module would allow to drop merge.js + tests -> less maintenance.

Provide a way to validate only a subset of the schema

My suggestion for an api:

schema.fields("name", "age", "location").validate(..);

Internally the fields()-method just creates a new object that inherits from the schema and overrides the key-field:

Schema.prototype.fields = function () {
    var subset = Object.create(this);

    subset.keys = Array.prototype.slice.call(arguments);

    return subset;
};

What do you think @meaku ?

match validator

It would be nice to have a match validator which is applicable to String types and evaluates regular expressions.

For example:

var panda = new Schema("Panda", {
  name: {
    type: String,
    match: /^A/,
    required: true
  }
});

The (custom) validator:

function match(regexp) {
    return function match() {
        return regexp.test(arguments[0]) || "regexp-mismatch"
    }
};

var panda = new Schema("Panda", {
  name: {
    type: String,
    validate: match(/^A/),
    required: true
});

What do you think @jhnns and @meaku ?

Use "validate" in favor of "validators"

I think the schema should only have one dedicated key for validators. I'd just use "validate" and kick the support for "validators". It makes always things more complicated if you can either use this or another style.

validate key value

It would be quite handy if it is possible to pass a key and the according value as arguments to validate().

Like this:

myCoolSchema.validate(key, value, function (validation) {
  if (validation.result) {
     console.log("I knew it!")
  }

  // ...
});

How to handle optional keys

I dont know if this an issue for alamid-schema but how can we handle an update request to delete an optional key. Alamid-schema doesnt know about requests, but maybe there should be a method to apply a diff. Or maybe this should be done by the model?

Extra keys

How do we handle extra keys? Are they allowed? Does it depend on the schema?

Also related to normalization #9 #10

make validators/casters type-aware

Mongoose has different implementations of most validators like required to check for the certain type. I think we should do it the same way.

Should not include Promise shim

I think it's bad practice that a lib brings its own shim. alamid-schema should just use the Promise constructor. It's up to the user to add this shim to the environment.

Add defaults

Should defaults be a plugin or work out of the box?

Add tests for builtin validators

The current tests only check if the builtin validators have been applied. But it should also be checked if they work correctly.

Proposal for loading plugins

I think it would be a more comfortable API for users of this module if plugins could be loaded like this

const Schema = require('alamid-schema');
Schema.use('validation');

rather than

const Schema = require('alamid-schema');
Schema.use(require("alamid-schema/plugins/validation"));

Imho this is more readable. The required steps to load this plugin can easily be done by alamid-schema.

alamid-schema should copy the definition before modifying it

It seems like mongoose cannot handle the normalized schema definition (for example type: "Array" will produce an error like "string is not a function"). Currently the schema definition needs to be copied on the application's side before passing it to alamid-schema.

Rename failedFields to errors

I know this seems like "stupid" discussion, but I think naming is important and short and descriptive names are always a sign of a good api.

Imho in the validation context using errors is sufficient. But this is somehow related to our discussion regarding whole model errors. If there are these kinds of errors errors would not be descriptive enough.

So we should discuss whole model errors first and then decide here.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.