Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

more string testing

  • Loading branch information...
commit f95ccbc4425edef6175d1fa0b47c9ac8506eb798 1 parent bc1de0c
@mwawrusch mwawrusch authored
View
56 lib/property_info.js
@@ -1,5 +1,5 @@
(function() {
- var _;
+ var StringPropertyConverter, _;
_ = require("underscore");
/**
Reprents a single property. Internally used to store mapping info and stuff.
@@ -47,12 +47,62 @@
This is where some of the magic happens.
*/
PropertyInfo.prototype.mongooseSchemaDefinition = function() {
+ var dt, res;
+ dt = this.propertySchema.type.toLowerCase();
+ switch (dt) {
+ case "string":
+ return new StringPropertyConverter().convert(this.propertySchema);
+ default:
+ res = {
+ type: this.moongooseDataType()
+ };
+ return res;
+ }
+ };
+ return PropertyInfo;
+ })();
+ /**
+ A property converter that converts string type.
+ */
+ StringPropertyConverter = (function() {
+ function StringPropertyConverter() {}
+ /**
+ The property schema that is accepted looks like this:
+ type: "String"
+ title:
+ description:
+ required:
+ default:
+
+
+ pattern: "" optional, a regex pattern
+ minLength: 0 optional, a positive integer
+ maxLength: 1000 optional, a positive integer
+ # Custom Properties
+ textTrimming: null || "trim"
+ textTransform: null || "uppercase" || "lowercase"
+ index, unique,sparse
+ ---
+ format:
+ enum:
+
+ */
+ StringPropertyConverter.prototype.convert = function(propertySchema) {
var res;
res = {
- type: this.moongooseDataType()
+ type: String,
+ title: propertySchema.title || null,
+ description: propertySchema.description || null,
+ required: !!propertySchema.required,
+ "default": propertySchema["default"] || null,
+ lowercase: false,
+ uppercase: false,
+ trim: false,
+ match: null,
+ "enum": null
};
return res;
};
- return PropertyInfo;
+ return StringPropertyConverter;
})();
}).call(this);
View
44 spec/extended_string-spec.coffee
@@ -0,0 +1,44 @@
+vows = require 'vows'
+assert = require 'assert'
+mongoose = require 'mongoose'
+
+main = require '../lib/index'
+specHelper = require './spec_helper'
+specHelper.connectDatabase()
+
+vows.describe("extended_string")
+ .addBatch
+ "CLEANUP TEMP":
+ topic: () ->
+ specHelper.cleanTmpFiles []
+ "THEN IT SHOULD BE CLEAN :)": () ->
+ assert.isTrue true
+ .addBatch
+ "CLEANING DATABASE" :
+ topic: () ->
+ specHelper.cleanDatabase @callback
+ return
+ "THEN IT SHOULD BE CLEAN :)": () ->
+ assert.isTrue true
+
+ .addBatch
+ "SETUP" :
+ topic: () ->
+ specHelper.setup @callback
+ return
+ "THEN IT SHOULD BE SET UP :)": () ->
+ assert.isTrue true
+ .addBatch
+ "WHEN adding a full blown string property and creating a model from it" :
+ topic: () ->
+ @xx = new main.JsonSchemaToMongoose()
+ json = specHelper.loadJsonFixture "extended_string.json"
+ @xx.addJsonSchema null, json, (err,newEntity)=>
+ @ExtendedString = @xx.mongooseModel("ExtendedString")
+ @callback(err,newEntity)
+
+ return
+ "THEN it should have been created": (err,newEntity) ->
+ assert.isNotNull @ExtendedString
+
+ .export module
View
23 spec/fixtures/extended_string.json
@@ -0,0 +1,23 @@
+{
+ "name" : "ExtendedString",
+ "description":"A string def with all the bells and whistles",
+ "type":"object",
+ "properties":{
+ "stringValue":{
+ "type":"string",
+ "title" : "ext. string",
+ "description":"A string value",
+ "required" : true,
+ "default": "Lindsay",
+ "pattern": "[\\w]*",
+ "minLength": 2,
+ "maxLength": 1000,
+ "textTrimming": "trim",
+ "textTransform" : "uppercase",
+ "index" : true,
+ "uniqueIndex" : true,
+ "sparseIndex" : true,
+ "enum": ["Lindsay","Ashley","Rihanna"]
+ }
+ }
+}
View
49 src/property_info.coffee
@@ -40,7 +40,52 @@ class exports.PropertyInfo
This is where some of the magic happens.
###
mongooseSchemaDefinition: () ->
- res =
- type : @moongooseDataType()
+ dt = @propertySchema.type.toLowerCase()
+ switch dt
+ when "string" then new StringPropertyConverter().convert(@propertySchema)
+ else
+ res =
+ type : @moongooseDataType()
+ res
+
+###*
+A property converter that converts string type.
+###
+class StringPropertyConverter
+
+ ###*
+ The property schema that is accepted looks like this:
+ type: "String"
+ title:
+ description:
+ required:
+ default:
+
+
+ pattern: "" optional, a regex pattern
+ minLength: 0 optional, a positive integer
+ maxLength: 1000 optional, a positive integer
+ # Custom Properties
+ textTrimming: null || "trim"
+ textTransform: null || "uppercase" || "lowercase"
+ index, unique,sparse
+ ---
+ format:
+ enum:
+
+ ###
+ convert: (propertySchema) ->
+ res =
+ type: String
+ title: propertySchema.title || null
+ description: propertySchema.description || null
+ required : !!propertySchema.required
+ default: propertySchema.default || null
+
+ lowercase: false
+ uppercase: false
+ trim:false
+ match:null
+ enum: null
res
Please sign in to comment.
Something went wrong with that request. Please try again.