7

After hourlong searching I have found no solution about my problem and now I hope someome could be able to help me here.

So basically what I am looking for is a way to generate a JavaScript file (*.js) from a JSON (Schema) file automatically using NodeJS. I know that there are things like fs.write, but this is surely no fitting way for my problem, I think. And so far I have found no other way to create my JavaScript file other than that.

Basically I want to translate:

{
"type":"object",
"properties": {
    "name": {
        "type": "string"
    },
    "age": {
        "type": "integer",
        "default":12
    },
    "favorite_color": {
        "type": "string"
    },
    "gender": {
        "type": "string",
        "enum": [
            "male",
            "female"
        ]
    }
}
}

Into JavaScript code like:

var data = function() { 

    data.baseConstructor.call(this);
    this.name = ko.observable("");
    this.age = ko.obseravble(12);
    this.favorite_color = ko.observable();
    this.gender = ko.observable(data.genderModes.male);

}

data.genderModes = {

   male: "male",
   female: "female" 
}

Would someone be able to give me a hint to my problem?

5
  • name and favorite_color have the same configuration but are generated differently Commented Feb 16, 2015 at 9:10
  • That does not help me with my problem/question Commented Feb 16, 2015 at 9:21
  • that was an observation so that you can clarify the rules for the generation. btw, what did you do so far that did not fit your problem? Commented Feb 16, 2015 at 9:26
  • Well, right now I am about to dynamically generate strings that would reflect the content of my .js file and I think that this is the only way to really do it Commented Feb 16, 2015 at 10:42
  • 2
    Sometimes this is just a lot of typing. Find a good find and replace option in your editor helps a lot (as does linting your code and running tests to make sure you didn't miss any). What are you hoping to achieve with this? What will all the .js files do? There might be a different approach to try if we knew what you were trying to do (give some bigger context). Commented Feb 16, 2015 at 17:50

1 Answer 1

2

I don't know how to convert your json to js function but if you want to create object from json schema, you could use json-schema-defaults. After creating one object with json-schema-defaults you can create anothers with Object.create and you can add first level properties to new objects which is created by Object.create function.

var a = require('json-schema-defaults')({
"type":"object",
"properties": {
    "name": {
        "type": "string"
    },
    "age": {
        "type": "integer",
        "default":12
    },
  }
});

var b = Object.create(a,{
                         id:{ value:1 }, 
                          f:{ value:function() { 
                                     console.log('run lola run');
                                    }
                            }
                         }
                       );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.