garycourt / JSV

A JavaScript implementation of an extendable, fully compliant JSON Schema validator.
618 stars 84 forks source link

patternProperies: regexp doesn't work - it should work? #52

Closed d-adamkiewicz closed 12 years ago

d-adamkiewicz commented 12 years ago

Hi, I'm not sure if it's bug or I'm doing something wrong

- the following code reports always success. It seems regexp isn't taken into account.

var JSV = require('JSV').JSV; var env = JSV.createEnvironment(); var json, schema; var result = env.validate(json = {Z:1}, schema = {type: 'object', patternProperties: {'[a-z]': {type:'number'}}}); console.log('\njson:\n' + JSON.stringify(json, null, 4) + '\nschema:\n' + JSON.stringify(schema, null, 4)); if (result.errors.length === 0) { console.log('success'); } else { console.log('failure'); } result = env.validate(json = {a:1}, schema = {type: 'object', patternProperties: {'[A-Z]': {type:'number'}}}); console.log('\njson:\n' + JSON.stringify(json, null, 4) + '\nschema:\n' + JSON.stringify(schema, null, 4)); if (result.errors.length === 0) { console.log('success'); } else { console.log('failure'); } result = env.validate(json = {a:1}, schema = {type: 'object', patternProperties: {'^a\d': {type:'number'}}}); console.log('\njson:\n' + JSON.stringify(json, null, 4) + '\nschema:\n' + JSON.stringify(schema, null, 4)); if (result.errors.length === 0) { console.log('success'); } else { console.log('failure'); }

garycourt commented 12 years ago

You need to use "additionalProperties" : false to cause errors to be thrown on undefined properties.