Skip to content

Commit

Permalink
feat(zone): added zone validation
Browse files Browse the repository at this point in the history
  • Loading branch information
msimerson committed Feb 7, 2024
1 parent 1fe8725 commit ef9dedd
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 11 deletions.
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ Validation class for objects in NicTool. Analgous to Nictool/../\*/Sanity in v2.
```
> npm test
group
name
✔ accepts valid
Expand Down Expand Up @@ -45,7 +44,7 @@ Validation class for objects in NicTool. Analgous to Nictool/../\*/Sanity in v2.
✔ rejects invalid character: #
✔ rejects invalid character: {
✔ rejects invalid character: }
✔ rejects invalid character:
✔ rejects invalid character:
✔ rejects if first character is not alphanumeric: -test
✔ rejects if first character is not alphanumeric: _test
Expand Down Expand Up @@ -141,9 +140,17 @@ d.com.
✔ rejects invalid: 1. .3.4
✔ rejects invalid: 1.2,3.4
✔ rejects invalid: 1.,.3.4
ttl
✔ rejects missing
shared
ttl
✔ accepts valid
✔ rejects missing
✔ accepts valid: 0
✔ accepts valid: 3600
✔ accepts valid: 86401
✔ accepts valid: 2147483647
✔ rejects invalid: -299
✔ rejects invalid: -2592001
✔ rejects invalid: -2
Expand Down Expand Up @@ -186,7 +193,7 @@ d.com.
✔ rejects invalid character: #
✔ rejects invalid character: {
✔ rejects invalid character: }
✔ rejects invalid character:
✔ rejects invalid character:
email
✔ accepts valid
Expand All @@ -198,7 +205,51 @@ d.com.
✔ rejects too short password
✔ rejects most common password strings
zone
zone
✔ accepts valid
✔ rejects empty
✔ rejects invalid: thisis~atest.com.
✔ rejects invalid: thisis`atest.com.
✔ rejects invalid: thisis!atest.com.
✔ rejects invalid: thisis@atest.com.
✔ rejects invalid: thisis$atest.com.
✔ rejects invalid: thisis%atest.com.
✔ rejects invalid: thisis^atest.com.
✔ rejects invalid: thisis&atest.com.
✔ rejects invalid: thisis*atest.com.
✔ rejects invalid: thisis(atest.com.
✔ rejects invalid: thisis)atest.com.
✔ rejects invalid: thisis+atest.com.
✔ rejects invalid: thisis=atest.com.
✔ rejects invalid: thisis[atest.com.
✔ rejects invalid: thisis]atest.com.
✔ rejects invalid: thisis\atest.com.
✔ rejects invalid: thisis/atest.com.
✔ rejects invalid: thisis|atest.com.
✔ rejects invalid: thisis?atest.com.
✔ rejects invalid: thisis>atest.com.
✔ rejects invalid: thisis<atest.com.
✔ rejects invalid: thisis"atest.com.
✔ rejects invalid: thisis'atest.com.
✔ rejects invalid: thisis:atest.com.
✔ rejects invalid: thisis;atest.com.
✔ rejects invalid: thisis,atest.com.
✔ rejects invalid: thisis#atest.com.
✔ rejects invalid: thisis{atest.com.
✔ rejects invalid: thisis}atest.com.
✔ rejects invalid: thisis atest.com.
✔ rejects invalid: thisis
atest.com.
nt_group_id
✔ accepts valid: 1
✔ rejects invalid:
✔ rejects invalid: 0
✔ rejects invalid: abc
ttl
✔ rejects missing
168 passing (35ms)
211 passing (40ms)
```
2 changes: 1 addition & 1 deletion lib/shared.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Joi = require('joi')

exports.nt_group_id = Joi.number().integer().positive();
exports.nt_group_id = Joi.number().integer().positive()

exports.permission = Joi.object({
group: Joi.object({
Expand Down
40 changes: 40 additions & 0 deletions lib/zone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const Joi = require('joi')

const shared = require('./shared')

exports.zone = Joi.object({
nt_group_id: Joi.number().integer().positive().required(),

// parent_group_id: Joi.number().integer().greater(-1),

zone: Joi.string()
.min(3)
.max(255)
.domain({ allowFullyQualified: true, tlds: false })
// .pattern(new RegExp("^[a-zA-Z0-9 _.@'-]+$"))
// .pattern(new RegExp('^[a-zA-Z0-9]'))
.required(),

description: Joi.string().empty(''),

mailaddr: Joi.string().empty(''),

minimum: Joi.number().integer().greater(-1).max(2147483647).required(),

nameservers: Joi.array().items(Joi.string()),

refresh: Joi.number().integer().greater(-1).max(2147483647).required(),

retry: Joi.number().integer().greater(-1).max(2147483647).required(),

expire: Joi.number().integer().greater(-1).max(2147483647).required(),

serial: Joi.number().integer().greater(-1).max(2147483647).required(),

ttl: shared.ttl.required(),

deleted: Joi.boolean(),

// has_children: Joi.boolean(),
// permission: permission.permission,
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nictool/nt-validate",
"version": "0.4.0",
"version": "0.5.0",
"description": "NicTool Object Validation",
"main": "index.js",
"directories": {
Expand Down
13 changes: 13 additions & 0 deletions test/fixtures/zone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"nt_group_id": 1,
"zone": "example.com",
"ttl": 1,
"serial": 20240404,
"nameservers": ["a.ns.example.com", "b.ns.example.com"],
"mailaddr": "hostmaster.example.com.",
"description": "test",
"refresh": 1,
"retry": 2,
"expire": 3,
"minimum": 4
}
9 changes: 4 additions & 5 deletions test/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@ const assert = require('node:assert/strict')
const shared = require('../lib/shared')

describe('shared', function () {

describe('ttl', function () {
it(`accepts valid`, () => {
const { error, value } = shared.ttl.validate(0)
const { error } = shared.ttl.validate(0)
assert.ifError(error)
})

it(`rejects missing`, () => {
const { error, value } = shared.ttl.validate('')
const { error } = shared.ttl.validate('')
assert.strictEqual(error.message, '"value" must be a number')
})

for (const ttl of [0, 3600, 86401, 2147483647]) {
it(`accepts valid: ${ttl}`, () => {
const { error, value } = shared.ttl.validate(ttl)
const { error } = shared.ttl.validate(ttl)
assert.ifError(error)
})
}
Expand All @@ -30,7 +29,7 @@ describe('shared', function () {

for (const ttl of [-299, -2592001, -2, -1, 2147483648, 'oops']) {
it(`rejects invalid: ${ttl}`, () => {
const { error, value } = shared.ttl.validate(ttl)
const { error } = shared.ttl.validate(ttl)
assert.ok(validErrs.includes(error.message))
})
}
Expand Down
89 changes: 89 additions & 0 deletions test/zone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const assert = require('node:assert/strict')

const schema = require('../lib/zone').zone
const testZone = require('./fixtures/zone.json')

describe('zone', function () {
describe('zone', function () {
it(`accepts valid`, () => {
const testCase = JSON.parse(JSON.stringify(testZone))

const { error, value } = schema.validate(testCase)

assert.ifError(error)
assert.deepStrictEqual(testCase, value)
})

it(`rejects empty`, () => {
const testCase = JSON.parse(JSON.stringify(testZone))
testCase.zone = ''

const { error, value } = schema.validate(testCase)

assert.strictEqual(error.message, '"zone" is not allowed to be empty')
assert.deepStrictEqual(testCase, value)
})

const invalid_chars = `~\`!@$%^&*()+=[]\\/|?><"':;,#{} \n`
.split('')
.map((a) => `thisis${a}atest.com.`)

for (const char of invalid_chars) {
it(`rejects invalid: ${char}`, () => {
const testCase = JSON.parse(JSON.stringify(testZone))
testCase.zone = char

const { error, value } = schema.validate(testCase)
// if (error) console.error(error.message)

assert.strictEqual(
error.message,
'"zone" must contain a valid domain name',
)
assert.deepStrictEqual(testCase, value)
})
}
})

describe('nt_group_id', function () {
for (const gid of [1]) {
it(`accepts valid: ${gid}`, () => {
const testCase = JSON.parse(JSON.stringify(testZone))
testCase.nt_group_id = gid

const { error, value } = schema.validate(testCase)

assert.ifError(error)
assert.deepStrictEqual(testCase, value)
})
}

const errMsgs = [
'"nt_group_id" must be a positive number',
'"nt_group_id" must be a number',
]
for (const gid of ['', 0, 'abc']) {
it(`rejects invalid: ${gid}`, () => {
const testCase = JSON.parse(JSON.stringify(testZone))
testCase.nt_group_id = gid

const { error, value } = schema.validate(testCase)

assert.ok(errMsgs.includes(error.message))
assert.deepStrictEqual(testCase, value)
})
}
})

describe('ttl', function () {
it(`rejects missing`, () => {
const testCase = JSON.parse(JSON.stringify(testZone))
delete testCase.ttl

const { error, value } = schema.validate(testCase)

assert.strictEqual(error.message, '"ttl" is required')
assert.deepStrictEqual(testCase, value)
})
})
})

0 comments on commit ef9dedd

Please sign in to comment.