Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
}
}
}
}
}
}
}
},
password: { type: 'string', uniforms: { type: 'password' } },
passwordNumeric: { type: 'number', uniforms: { type: 'password' } }
},
required: ['dateOfBirth']
};
const validator = jest.fn();
const bridge = new JSONSchemaBridge(schema, validator);
describe('#constructor()', () => {
it('sets schema correctly when has top level type of object', () => {
expect(bridge.schema).toEqual(schema);
});
it('sets schema correctly when has top level $ref', () => {
const localSchema = {
definitions: schema.definitions,
$ref: '#/definitions/personalData'
};
const localBridge = new JSONSchemaBridge(localSchema, validator);
expect(localBridge.schema).toEqual({
...localSchema,
...localSchema.definitions.personalData
});
it('throws when resolving field schema is not possible', () => {
const localBridge = new JSONSchemaBridge(
{ definitions: schema.definitions, $ref: '#/definitions/personalData' },
validator
);
expect(() => localBridge.getField('invalid')).toThrow(
/Field not found in schema/
);
});
});
it('works on top level when schema does not have properties', () => {
const localBridge = new JSONSchemaBridge(
{ definitions: schema.definitions, $ref: '#/definitions/lastName' },
validator
);
expect(localBridge.getSubfields()).toEqual([]);
});
});
it('falls back to input schema', () => {
const localSchema = { definitions: schema.definitions };
const localBridge = new JSONSchemaBridge(localSchema, validator);
expect(localBridge.schema).toEqual(localSchema);
});
});
it('always returns false', () => {
expect(JSONSchemaBridge.check()).not.toBeTruthy();
expect(JSONSchemaBridge.check(bridge)).not.toBeTruthy();
expect(JSONSchemaBridge.check(schema)).not.toBeTruthy();
expect(JSONSchemaBridge.check(validator)).not.toBeTruthy();
});
});
it('always returns false', () => {
expect(JSONSchemaBridge.check()).not.toBeTruthy();
expect(JSONSchemaBridge.check(bridge)).not.toBeTruthy();
expect(JSONSchemaBridge.check(schema)).not.toBeTruthy();
expect(JSONSchemaBridge.check(validator)).not.toBeTruthy();
});
});
it('always returns false', () => {
expect(JSONSchemaBridge.check()).not.toBeTruthy();
expect(JSONSchemaBridge.check(bridge)).not.toBeTruthy();
expect(JSONSchemaBridge.check(schema)).not.toBeTruthy();
expect(JSONSchemaBridge.check(validator)).not.toBeTruthy();
});
});
it('always returns false', () => {
expect(JSONSchemaBridge.check()).not.toBeTruthy();
expect(JSONSchemaBridge.check(bridge)).not.toBeTruthy();
expect(JSONSchemaBridge.check(schema)).not.toBeTruthy();
expect(JSONSchemaBridge.check(validator)).not.toBeTruthy();
});
});
function createValidator(schema) {
const validator = ajv.compile(schema);
return model => {
validator(model);
if (validator.errors && validator.errors.length) {
throw { details: validator.errors };
}
};
}
const schemaValidator = createValidator(schema);
export default new JSONSchemaBridge(schema, schemaValidator);
function createValidator(schema) {
const validator = ajv.compile(schema);
return model => {
validator(model);
if (validator.errors && validator.errors.length) {
throw { details: validator.errors };
}
};
}
const schemaValidator = createValidator(schema);
export default new JSONSchemaBridge(schema, schemaValidator);