Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const manuelstofer = require("json-pointer"); // https://github.com/manuelstofer/json-pointer
const flitbit = require("json-ptr"); // https://github.com/flitbit/json-ptr
const doc = {
foo: [1, 2, 3, 4], // eslint-disable-line no-magic-numbers
baz: [
{
qux: "hello",
},
],
};
const pointer = "/baz/0/qux";
const compiled = ooPointer.compile(pointer);
const janlCompiled = jsonpointer.compile(pointer);
const evaluate = jsonpointerjs.get(doc);
const ptr = flitbit.create(pointer);
const suite = benchmark.Suite("pointer");
suite
.add("find", function() {
ooPointer.find(doc, pointer);
})
.add("compiled", function() {
compiled(doc);
})
.add("compile + compiled", function() {
ooPointer.compile(pointer)(doc);
export const filterFormData = (data: any, model: any): any => {
const filteredResult: any = {};
const rootKey = Object.keys(model.properties)[0];
const modelPath = model.properties[rootKey].$ref.slice(1);
const pointer = JsonPointer.compile(modelPath);
const root: any = pointer.get(model);
Object.keys(data).forEach((key: string) => {
const formDataKey = getKeyWithoutIndex(key);
const formDataRoot = formDataKey.split('.')[0];
const element = root.properties[formDataRoot];
if (element && (!element['@xsdType'] || element['@xsdType'] !== 'XmlAttribute')) {
filteredResult[key] = data[key];
}
});
return filteredResult;
};
export function createValidator(schema: any): ISchemaValidator {
const ajv = new Ajv({ allErrors: true, coerceTypes: true });
ajv.addFormat('year', /^[0-9]{4}$/);
ajv.addSchema(schema, 'schema');
const rootKey = Object.keys(schema.properties)[0];
const rootElementPath = schema.properties[rootKey].$ref;
const rootPtr = JsonPointer.compile(rootElementPath.substr(1));
const rootElement = rootPtr.get(schema);
const schemaValidator: ISchemaValidator = {
validator: ajv,
schema,
rootElement,
rootElementPath,
};
return schemaValidator;
}
const dataModelRoot = dataModelPath[0];
if (subSchema.properties && subSchema.properties[dataModelRoot] && dataModelPath && dataModelPath.length !== 0) {
const localRootElement = subSchema.properties[dataModelRoot];
if (localRootElement.$ref) {
const childSchemaPtr = JsonPointer.compile(localRootElement.$ref.substr(1));
return getSchemaPart(dataModelPath.slice(1), childSchemaPtr.get(mainSchema), mainSchema);
}
if (localRootElement.items && localRootElement.items.$ref) {
const childSchemaPtr = JsonPointer.compile(localRootElement.items.$ref.substr(1));
return getSchemaPart(dataModelPath.slice(1), childSchemaPtr.get(mainSchema), mainSchema);
}
return localRootElement;
}
if (subSchema.$ref) {
const ptr = JsonPointer.compile(subSchema.$ref.substr(1));
return getSchemaPart(dataModelPath.slice(1), ptr.get(mainSchema), mainSchema);
}
return subSchema;
}
matches(rel) {
if (rel && !rel.constructor === String) {
return false
}
try {
return jsonPointer.compile(rel) instanceof Object
} catch (e) {
return false
}
}
export function getSchemaPart(dataModelPath: string[], subSchema: any, mainSchema: any) {
const dataModelRoot = dataModelPath[0];
if (subSchema.properties && subSchema.properties[dataModelRoot] && dataModelPath && dataModelPath.length !== 0) {
const localRootElement = subSchema.properties[dataModelRoot];
if (localRootElement.$ref) {
const childSchemaPtr = JsonPointer.compile(localRootElement.$ref.substr(1));
return getSchemaPart(dataModelPath.slice(1), childSchemaPtr.get(mainSchema), mainSchema);
}
if (localRootElement.items && localRootElement.items.$ref) {
const childSchemaPtr = JsonPointer.compile(localRootElement.items.$ref.substr(1));
return getSchemaPart(dataModelPath.slice(1), childSchemaPtr.get(mainSchema), mainSchema);
}
return localRootElement;
}
if (subSchema.$ref) {
const ptr = JsonPointer.compile(subSchema.$ref.substr(1));
return getSchemaPart(dataModelPath.slice(1), ptr.get(mainSchema), mainSchema);
}
return subSchema;
}