Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Common Hooks validateschema error #229

Closed
rjpalermo1 opened this issue Jul 10, 2017 · 7 comments
Closed

Common Hooks validateschema error #229

rjpalermo1 opened this issue Jul 10, 2017 · 7 comments
Labels

Comments

@rjpalermo1
Copy link

Steps to reproduce

https://docs.feathersjs.com/api/hooks-common.html#validateschema fresh install inserted in CREATE user-hooks.js validate schema as per the link above.

module.exports = {
before: {
all: [],
find: [ authenticate('jwt') ],
get: [ ...restrict ],
create: [ validateSchema(userSchema(), Ajv), hashPassword(), setCreatedAt(), setUpdatedAt() ],
update: [ ...restrict, hashPassword() ],
patch: [ ...restrict, hashPassword() ],
remove: [ ...restrict ]
},

function userSchema() { return { title: 'User Schema', $schema: 'http://json-schema.org/draft-04/schema#', type: 'object', required: [ 'email', 'password', 'role' ], additionalProperties: false, properties: { email: { type: 'string', maxLength: 100, minLength: 6 }, password: { type: 'string', maxLength: 30, minLength: 8 }, role: { type: 'string' } } }; }
ERROR
/Users/me/Develop/feathers/src/services/users/users.hooks.js:22 create: [ validateSchema(userSchema(), Ajv), hashPassword(), setCreatedAt(), setUpdatedAt() ], ReferenceError: Ajv is not defined

I added const Ajv = require('ajv');

New error:

`
/Users/Tektango/Develop/feathers/node_modules/ajv/lib/ajv.js:94
if (!v) throw new Error('no schema with key or ref "' + schemaKeyRef + '"');

Error: no schema with key or ref "http://json-schema.org/draft-04/schema#"
`

https://github.com/epoberezkin/ajv says

JSON Schema draft-06 is published.

Ajv version 5.0.0 that supports draft-06 is released. It may require either migrating your schemas or updating your code (to continue using draft-04 and v5 schemas).

Please note: To use Ajv with draft-04 schemas you need to explicitly add meta-schema to the validator instance:

ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));

@eddyystop
Copy link
Collaborator

ReferenceError: Ajv is not defined It seems you didn't require Ajv as shown in the doc example https://docs.feathersjs.com/api/hooks-common.html#validateschema .

@rjpalermo1
Copy link
Author

Ok - I'll dig into it deeper.

I reported that I added const Ajv = require('ajv'); but looking at docs again and try to figure out what else I am missing...

@criticalmaas
Copy link

I'm having the same issue. I solved the error message by updating the schema version in my JSON Schema but I still cannot get validation to happen.

change from v04
$schema: 'http://json-schema.org/draft-04/schema#'
to v06 in your JSON Schema file
$schema: 'http://json-schema.org/draft-06/schema#'

Per the upgrade to AJV 5.0.0
https://github.com/epoberezkin/ajv/releases/tag/5.0.0

@eddyystop
Copy link
Collaborator

but I still cannot get validation to happen. Does the hook not work, or does Ajv not agree with your schema?

@criticalmaas
Copy link

Right, the hook does not work for two reasons. First is the schema 04 to 06 change needed and second is validation succeeds all the time. I managed to fix by rewriting my own and I intend to debug the common hook to see why it is not working.

//needs cleanup ;o)
const Ajv = require('ajv');
let schema = require('./schema.json');
let feathersErrors = require('feathers-errors');

let errors = feathersErrors.errors;

function validate(hook){
  let ajv = new Ajv({ allErrors: true });
  let validate = ajv.compile(schema);
  let valid = validate(hook.data);

  if (!valid) {
    throw new errors.BadRequest('Invalid Request', validate.errors);
  }
}

@eddyystop
Copy link
Collaborator

This hooks is being used in production so I look forward to what you find. Thanks!

@eddyystop
Copy link
Collaborator

This has been consolidated with #372

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants