Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'json-schema-ref-parser' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
import path from 'path';
import gql from 'graphql-tag';
import swaggerToSchema from './index';
import createFakerResolver from './createFakerResolver';
import createHttpResolver from './createHttpResolver';
import RefParser from 'json-schema-ref-parser/lib/index';
import perfy from 'perfy';
const PERF_LABEL = 'perf';
// RefParser.dereference(path.resolve(__dirname, './test/fixtures/cd.json'))
RefParser.dereference('http://api-gateway/api/v2/swagger.json')
.then(
(cdSwagger) => {
console.log('Start');
perfy.start(PERF_LABEL);
// swaggerToSchema({ swagger: cdSwagger, createResolver: createFakerResolver });
swaggerToSchema({ swagger: cdSwagger, createResolver: createHttpResolver });
console.log('End');
// console.log(JSON.stringify(result.data.__schema.types.length, null, 2));
console.log(perfy.end(PERF_LABEL).time);
process.exit();
}
);
import Service from '@ember/service';
import RefParser from 'json-schema-ref-parser';
import request from 'ember-ajax/request';
import { task, all, timeout } from 'ember-concurrency';
import { inject as service } from '@ember/service';
import {
// isAjaxError,
isNotFoundError,
// isForbiddenError
} from 'ember-ajax/errors';
import semver from 'semver';
const parser = new RefParser();
export default Service.extend({
init() {
this._super(...arguments);
/**
* Instance of JSON Schema $Ref Parser
*
* @method parser
* @protected
* @return {Object}
*/
this.parser = parser;
},
flashMessages: service(),
fetchSchemas: task(function* (url) {
// Because of this issue ( https://github.com/APIDevTools/json-schema-ref-parser/issues/101#issuecomment-421755168 )
// We need this workaround ( use '$RefParser.dereference' instead of '$RefParser.bundle' ) if asked by user
if (typeof filePath === 'string') {
const origCwd = process.cwd();
const specDir = path.resolve(origCwd, path.dirname(filePath));
const absolutePath = path.resolve(origCwd, filePath);
if (fs.existsSync(absolutePath)) {
// Get document, or throw exception on error
try {
process.chdir(specDir);
const docWithRefs = jsYaml.safeLoad(
fs.readFileSync(absolutePath, 'utf8'),
{ json: true },
);
return $refParser.mode === 'dereference'
? $RefParser.dereference(docWithRefs)
: $RefParser.bundle(docWithRefs);
} finally {
process.chdir(origCwd);
}
} else {
throw new Error(
`${this.loggingPrefix}spec could not be read at ${filePath}`,
);
}
}
return $refParser.mode === 'dereference'
? $RefParser.dereference(filePath)
: $RefParser.bundle(filePath);
}
const uuid62 = require('uuid62');
const uuidv4 = require('uuid/v4');
const refParser = require('json-schema-ref-parser');
const mergeAllOf = require('json-schema-merge-allof');
const utils = require('../utils');
const config = require('../../config');
const { validate } = require('../../../lib/schemas');
const credentialDao = require('./credential.dao.js');
const s = {};
const dereferencePromise = refParser.dereference(config.models.credentials).then(derefSchema => mergeAllOf(derefSchema));
s.insertScopes = function (scopes) {
return validateNewScopes(scopes)
.then(newScopes => {
if (!newScopes) {
return true; // no scopes to insert
}
return credentialDao.insertScopes(newScopes).then(v => !!v);
});
};
s.removeScopes = function (scopes) {
return credentialDao.removeScopes(scopes).then(v => !!v);
};
// We need this workaround ( use '$RefParser.dereference' instead of '$RefParser.bundle' ) if asked by user
if (typeof filePath === 'string') {
const origCwd = process.cwd();
const specDir = path.resolve(origCwd, path.dirname(filePath));
const absolutePath = path.resolve(origCwd, filePath);
if (fs.existsSync(absolutePath)) {
// Get document, or throw exception on error
try {
process.chdir(specDir);
const docWithRefs = jsYaml.safeLoad(
fs.readFileSync(absolutePath, 'utf8'),
{ json: true },
);
return $refParser.mode === 'dereference'
? $RefParser.dereference(docWithRefs)
: $RefParser.bundle(docWithRefs);
} finally {
process.chdir(origCwd);
}
} else {
throw new Error(
`${this.loggingPrefix}spec could not be read at ${filePath}`,
);
}
}
return $refParser.mode === 'dereference'
? $RefParser.dereference(filePath)
: $RefParser.bundle(filePath);
}
taskEither.tryCatch(async () => {
const cwd = options.cwd || process.cwd();
const out = path.isAbsolute(options.out) ? options.out : path.resolve(cwd, options.out);
const spec = path.isAbsolute(options.spec) ? options.spec : path.resolve(cwd, options.spec);
log('Processing', spec);
const $refs = await $RefParser.resolve(spec, {
dereference: {
circular: 'ignore',
},
parse: {
sketch: sketchParser121,
},
});
const specs: Record = pipe(
Object.entries($refs.values()),
array.reduce({}, (acc, [fullPath, schema]) => {
const isRoot = fullPath === spec;
const relative = path.relative(cwd, fullPath);
// skip specLike check for root because it should always be decoded with passed decoder and fail
if (!isRoot && isLeft(specLikeCodec.decode(schema))) {
log('Unable to decode', relative, 'as spec. Treat it as an arbitrary json.');
function convert(program, callback) {
// define no-op callback
if (!callback) {
callback = function (err, blueprint) { }
}
// Read in the file
refparser.parse(program.input, function (err, schema) {
if (err) isMain ? halt(err) : callback(err);
// Read in aglio options
var options = {
themeTemplate: (program.themeTemplate === 'triple' || program.themeTemplate === 'index' || !program.themeTemplate) ?
path.resolve(__dirname, 'templates/' + (program.themeTemplate || 'index') + '.jade') :
themeTemplate,
themeVariables: program.themeVariables,
themeFullWidth: program.themeFullWidth,
noThemeCondense: program.noThemeCondense,
themeStyle: program.themeStyle,
locals: {
livePreview: program.server,
host: ((schema.schemes && schema.schemes.length > 0 && schema.host) ?
(schema.schemes[0] || 'https')
: 'https')
+ '://' + schema.host + (schema.basePath || '')
SwaggerParser.prototype.parse = async function (path, api, options, callback) {
let args = normalizeArgs(arguments);
args.options = new Options(args.options);
try {
let schema = await $RefParser.prototype.parse.call(this, args.path, args.schema, args.options);
if (schema.swagger) {
// Verify that the parsed object is a Swagger API
if (schema.swagger === undefined || schema.info === undefined || schema.paths === undefined) {
throw ono.syntax(`${args.path || args.schema} is not a valid Swagger API definition`);
}
else if (typeof schema.swagger === "number") {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('Swagger version number must be a string (e.g. "2.0") not a number.');
}
else if (typeof schema.info.version === "number") {
// This is a very common mistake, so give a helpful error message
throw ono.syntax('API version number must be a string (e.g. "1.0.0") not a number.');
}
else if (schema.swagger !== "2.0") {
throw ono.syntax(`Unrecognized Swagger version: ${schema.swagger}. Expected 2.0`);
it(swagger + ' should be completely resolvable.', function (done) {
RefParser.bundle(swagger, function (bundleErr: { readonly message: unknown }, _bundleResult: unknown) {
if (bundleErr) {
var msg = swagger + ' has references that cannot be resolved. They are as follows: \n' + util.inspect(bundleErr.message, { depth: null });
console.log(msg);
throw new Error(msg);
}
done();
});
});
}
schemas: {
SecuritySchemeModel: {
type: 'http',
scheme: 'basic',
descriptions: 'example text for def with unused security def'
}
},
securitySchemes: {
scheme1: {
$ref: '#/components/schemas/SecuritySchemeModel'
}
}
}
};
const resolvedSpec = await resolver.dereference(spec);
const res = validate({ resolvedSpec, isOAS3: true }, config);
expect(res.errors.length).toEqual(0);
expect(res.warnings.length).toEqual(1);
expect(res.warnings[0].message).toEqual(
'A security scheme is defined but never used: scheme1'
);
});
it('should warn about an unused security definition', function() {