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

added ipv4 and ipv6 validation #843

Merged
merged 5 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/languageservice/parser/jsonParser07.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ export const formats = {
errorMessage: localize('emailFormatWarning', 'String is not an e-mail address.'),
pattern: /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
},
ipv4: {
errorMessage: localize('ipv4FormatWarning', 'String does not match IPv4 format.'),
pattern: /^((25[0-5]|(2[0-4]|1\d|[1-9]|)\d)\.?\b){4}$/,
},
ipv6: {
errorMessage: localize('ipv6FormatWarning', 'String does not match IPv6 format.'),
pattern: /^([0-9a-f]|:){1,4}(:([0-9a-f]{0,4})*){1,7}$/i,
},
};

export const YAML_SOURCE = 'YAML';
Expand Down Expand Up @@ -1054,9 +1062,11 @@ function validate(
case 'date':
case 'time':
case 'email':
case 'ipv4':
case 'ipv6':
msivasubramaniaan marked this conversation as resolved.
Show resolved Hide resolved
{
const format = formats[schema.format];
if (!node.value || !format.pattern.exec(node.value)) {
if (!node.value || !format.pattern.test(node.value)) {
validationResult.problems.push({
location: { offset: node.offset, length: node.length },
severity: DiagnosticSeverity.Warning,
Expand Down
64 changes: 64 additions & 0 deletions test/schemaValidation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,70 @@ obj:
assert.deepStrictEqual(result[0].message, 'String does not match the pattern of "^.*\\$\\{\\{(.|[\r\n])*\\}\\}.*$".');
});

it('schema should validate ipv4 format - Negative Case', async () => {
const schema = {
type: 'array',
items: {
type: 'string',
format: 'ipv4',
},
};
languageService.addSchema(SCHEMA_ID, schema);
const content = `- 10.15.12.500`;
const result = await parseSetup(content);
expect(result).to.be.not.empty;
expect(telemetry.messages).to.be.empty;
expect(result.length).to.eq(1);
assert.deepStrictEqual(result[0].message, 'String does not match IPv4 format.');
});

it('schema should validate ipv4 format - Positive Case', async () => {
const schema = {
type: 'array',
items: {
type: 'string',
format: 'ipv4',
},
};
languageService.addSchema(SCHEMA_ID, schema);
const content = `- 255.255.255.255`;
const result = await parseSetup(content);
expect(result).to.be.empty;
expect(telemetry.messages).to.be.empty;
});

it('schema should validate ipv6 format - Negative Case', async () => {
const schema = {
type: 'array',
items: {
type: 'string',
format: 'ipv6',
},
};
languageService.addSchema(SCHEMA_ID, schema);
const content = `- 10.15.12.500`;
const result = await parseSetup(content);
expect(result).to.be.not.empty;
expect(telemetry.messages).to.be.empty;
expect(result.length).to.eq(1);
assert.deepStrictEqual(result[0].message, 'String does not match IPv6 format.');
});

it('schema should validate ipv6 format - Positive Case', async () => {
const schema = {
type: 'array',
items: {
type: 'string',
format: 'ipv6',
},
};
languageService.addSchema(SCHEMA_ID, schema);
const content = `- 2001:0db8:85a3:0000:0000:8a2e:0370:7334\n- 2001:0db8:85a3:0000:0000:8a2e:0370:7334\n- FEDC:BA98:7654:3210:FEDC:BA98:7654:3210\n- 1080::8:800:200C:417A\n- FF01::101\n- ::1`;
const result = await parseSetup(content);
expect(result).to.be.empty;
expect(telemetry.messages).to.be.empty;
});

it('should handle not valid schema object', async () => {
const schema = 'Foo';
languageService.addSchema(SCHEMA_ID, schema as JSONSchema);
Expand Down