How to use the js-yaml.JSON_SCHEMA function in js-yaml

To help you get started, we’ve selected a few js-yaml examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Surnet / swagger-jsdoc / bin / swagger-jsdoc.js View on Github external
fs.writeFile(fileName, swaggerSpec, err => {
    if (err) {
      throw err;
    }
    console.log('Swagger specification is ready.');
  });
}

function loadJsSpecification(data, resolvedPath) {
  // eslint-disable-next-line
  return require(resolvedPath);
}

const YAML_OPTS = {
  // OpenAPI spec mandates JSON-compatible YAML
  schema: jsYaml.JSON_SCHEMA,
};

function loadYamlSpecification(data) {
  return jsYaml.load(data, YAML_OPTS);
}

const LOADERS = {
  '.js': loadJsSpecification,
  '.json': JSON.parse,
  '.yml': loadYamlSpecification,
  '.yaml': loadYamlSpecification,
};

// Get an object of the definition file configuration.
function loadSpecification(defPath, data) {
  const resolvedPath = path.resolve(defPath);
github Surnet / swagger-jsdoc / bin / swagger-jsdoc.js View on Github external
function createSpecification(swaggerDefinition, apis, fileName) {
  // Options for the swagger docs
  const options = {
    // Import swaggerDefinitions
    swaggerDefinition,
    // Path to the API docs
    apis,
  };

  // Initialize swagger-jsdoc -> returns validated JSON or YAML swagger spec
  let swaggerSpec;
  const ext = path.extname(fileName);

  if (ext === '.yml' || ext === '.yaml') {
    swaggerSpec = jsYaml.dump(swaggerJSDoc(options), {
      schema: jsYaml.JSON_SCHEMA,
      noRefs: true,
    });
  } else {
    swaggerSpec = JSON.stringify(swaggerJSDoc(options), null, 2);
  }

  fs.writeFile(fileName, swaggerSpec, err => {
    if (err) {
      throw err;
    }
    console.log('Swagger specification is ready.');
  });
}
github alibaba / funcraft / lib / commands / config.js View on Github external
}
  if (newProf.accessKeySecret === markedAccessKeySecret) {
    newProf.accessKeySecret = profile.accessKeySecret;
  }

  const configDir = path.join(os.homedir(), '.fcli');

  const profPath = path.join(configDir, 'config.yaml');
  const isExists = await exists(profPath);

  var profYml;

  if (isExists) {
    const profContent = await readFile(profPath, 'utf8');
    profYml = yaml.safeLoad(profContent, {
      schema: yaml.JSON_SCHEMA
    });
    profYml.endpoint = `https://${newProf.accountId}.${newProf.defaultRegion}.fc.aliyuncs.com`;
    profYml.access_key_id = newProf.accessKeyId;
    profYml.access_key_secret = newProf.accessKeySecret;
    profYml.sls_endpoint = `${newProf.defaultRegion}.log.aliyuncs.com`;
    profYml.timeout = newProf.timeout;
    profYml.retries = newProf.retries;
    profYml.report = newProf.report;

    if (!isShortDateStr(profYml.api_version)) {
      // 1. fcli 默认配置的格式为 api_version: 2016-08-15
      // 2. js-yaml 在没有配置 schema: yaml.JSON_SCHEMA 时,会将其按照日期解析
      // 3. js-yaml dump 后,生成的内容为: 2016-08-15T00:00:00.000Z,然后会被写入 config.yaml
      // 4. fcli 读取到这个配置会导致请求出错

      // 这里做到了以下三种格式,最终都被格式化成 ‘2016-08-15’:
github jessica-taylor / hashlattice / lib / yaml.js View on Github external
} else {
      callback(null, self.runFunction(fref.fn, self.dataMap[fref.datatype][fref.filename]));
    }
  });
};

// Loads the data in a YAML file, allowing it to refer to other files.
function loadYamlFile(filename, callback) {
  var reader = new YamlReader();
  reader.evalFileRef(new FileRef('include', 'yaml', filename), callback);
}

// minimal schema for reading/writing from strings, with no file references
// JSON + binary
var minimalSchema = JsYaml.Schema.create(
    [JsYaml.JSON_SCHEMA],
    [BINARY_TYPE]);

// Converts a YAML string to data.
function yamlToData(yaml) {
  return JsYaml.safeLoad(yaml, {schema: fileRefSchema});
}

// Converts data to a YAML string.
function dataToYaml(data) {
  return JsYaml.safeDump(data, {schema: fileRefSchema});
}

module.exports = {
  FileRef: FileRef,
  loadYamlFile: loadYamlFile,
  yamlToData: yamlToData,
github ubc / compair / node_modules / bower / node_modules / update-notifier / node_modules / configstore / configstore.js View on Github external
get: function () {
			try {
				return yaml.safeLoad(fs.readFileSync(this.path, 'utf8'), {
					filename: this.path,
					schema: yaml.JSON_SCHEMA
				});
			} catch (err) {
				// create dir if it doesn't exist
				if (err.code === 'ENOENT') {
					mkdirp.sync(path.dirname(this.path), defaultPathMode);
					return {};
				}

				// improve the message of permission errors
				if (err.code === 'EACCES') {
					err.message = err.message + '\n' + permissionError + '\n';
				}

				// empty the file if it encounters invalid YAML
				if (err.name === 'YAMLException') {
					fs.writeFileSync(this.path, '', writeFileOptions);
github jessica-taylor / hashlattice / src / lib / yaml.js View on Github external
async evalFileRef(fref) {
    await this.load(fref.datatype, fref.filename);
    return await this.runFunction(fref.fn, this.dataMap[fref.datatype][fref.filename]);
  }
}

// Loads the data in a YAML file, allowing it to refer to other files.
async function loadYamlFile(filename) {
  var reader = new YamlReader();
  return await reader.evalFileRef(new FileRef('include', 'yaml', filename));
}

// minimal schema for reading/writing from strings, with no file references
// JSON + binary
var minimalSchema = JsYaml.Schema.create(
    [JsYaml.JSON_SCHEMA],
    [BINARY_TYPE]);

// Converts a YAML string to data.
function yamlToData(yaml) {
  return JsYaml.safeLoad(yaml, {schema: fileRefSchema});
}

// Converts data to a YAML string.
function dataToYaml(data) {
  return JsYaml.safeDump(data, {schema: fileRefSchema});
}

module.exports = {
  FileRef: FileRef,
  loadYamlFile: loadYamlFile,
  yamlToData: yamlToData,
github alibaba / serverless-vscode / src / utils / config.ts View on Github external
export function getConfig() {
  if (!fs.existsSync(configFilePath)) {
    vscode.window.showErrorMessage('Please run fun config first');
    return null;
  }
  const configContent = fs.readFileSync(configFilePath, 'utf8');
  const config = yaml.safeLoad(configContent, { schema: yaml.JSON_SCHEMA });
  return config;
}
github alibaba / funcraft / lib / nas / support.js View on Github external
async function readFileFromNasYml(nasYmlPath) {

  if (!await fs.pathExists(nasYmlPath)) {
    return {};
  }
  const contentStr = await fs.readFile(nasYmlPath, 'utf8');

  if (!contentStr) { return {}; }

  let contentObj;

  try {
    contentObj = yaml.safeLoad(contentStr, {
      schema: yaml.JSON_SCHEMA
    });
  } catch (e) {
    throw new Error(`\nThere was a problem with parsing ${nasYmlPath}. Ensure it is valid YAML!
${e}

After .nas.yml is configured correctly. You may be able to redeploy resources by following two steps:

1. Execute 'fun nas sync' to upload local NAS resources to the NAS service.
2. Execute ‘fun deploy’ to deploy resources to function computer.
`);
  }

  return contentObj || {};
}
github mangalam-research / wed / gulptasks / gulpfile.js View on Github external
.pipe(es.mapSync((file) => {
      file.contents = Buffer.from(JSON.stringify(yaml.safeLoad(file.contents, {
        schema: yaml.JSON_SCHEMA,
      })));

      return file;
    }))
    .pipe(gulp.dest(dest));
github simon-barton / node-abac / lib / policy.js View on Github external
_.forEach(policy_paths, path =>
    {
        switch (Path.extname(path))
        {
            case '.json':
                fileContent = JSON.parse(Fs.readFileSync(path).toString());
                break;
            case '.yml':
                fileContent = Yaml.safeLoad(Fs.readFileSync(path, 'utf8'),
                    { json: true, schema: Yaml.JSON_SCHEMA });
                break;
            default:
                fileContent = {};
        }

        policy = _.merge(policy, fileContent);
    });