Opened 6 years ago

Last modified 2 years ago

#16284 assigned enhancement

dojox.json.schema.validate schema type may be constructors

Reported by: Greg Girty Owned by: Kris Zyp
Priority: undecided Milestone: 1.15
Component: Dojox Version: 1.8.1
Keywords: Cc:
Blocked By: Blocking:

Description

This is a continuation of the patch submitted for #16279.

This enhances the behavior of dojox.json.schema.validate, it remains compatible with existing tests.

This revision allows the schema.type parameter to be a constructor function. The tested object will pass validation if it is an instance of schema.type.

E.g.

require(['dojo', 'dojox/json/schema'], function(dojo, djs){

/* Sample schema, note the use of Date in the inner 'type' param */

var s = {properties:{foo:
    {type:'object', 
    optional:false, 
    properties:{bar:{type:['null', Date]}, } 
    }
}}

/* Sample data */
var tests = [
    {expect: false, test: {foo:null} },
    {expect: false, test: {foo:{bar:'foo'}} },
    {expect: false, test: {foo:{bar:{}}} },
    {expect: true,  test: {foo:{bar:new Date()}} },
    {expect: true,  test: {foo:{bar:null}} }
]

for(var i in tests){
    var res = djs.validate(tests[i].test, s);
    console.log({expected:tests[i].expect, got:res.valid});
}
    
/* 
var alt = {properties:{foo:
    {type:'object', 
    optional:false, 
    properties:{bar:{type:Date, optional:true}, } 
    }
}} 

for(var i in tests){
    var res = djs.validate(tests[i].test, alt);
    console.log({expected:tests[i].expect, got:res.valid});
}
*/    
});

This revision also tests if the schema.type is null or empty string. For these cases, validation fails with an 'invalid schema' feedback.

Attachments (1)

schema.js (8.5 KB) - added by Greg Girty 6 years ago.
validate schema type params can be constructor functions

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by Greg Girty

Attachment: schema.js added

validate schema type params can be constructor functions

comment:1 Changed 6 years ago by bill

Component: GeneralDojox
Owner: set to Kris Zyp
Status: newassigned

comment:2 Changed 3 years ago by dylan

Milestone: tbd1.12

Will consider for 1.12.

comment:3 Changed 2 years ago by dylan

Milestone: 1.131.15

Ticket planning... move current 1.13 tickets out to 1.15 to make it easier to move tickets into the 1.13 milestone.

Note: See TracTickets for help on using tickets.