Skip to content

Commit

Permalink
Release v0.6.3 (#5)
Browse files Browse the repository at this point in the history
- user: permit @ character in username
- import .release submodule
- doc(CHANGELOG): added
- feat(user): exports v2 & v3 schemas
- run tests with shell script
- reduce duplicate workflow runs
  • Loading branch information
msimerson committed Feb 23, 2024
1 parent e5e970d commit fd1edbf
Show file tree
Hide file tree
Showing 27 changed files with 310 additions and 156 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ name: CI

on:
push:
branches-ignore:
- 'dependabot/**' #avoid duplicates: run the PR, not the push
tags-ignore:
- 'v*' #avoid rerun existing commit on release
pull_request:

env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
paths:
- package.json
release:
types: [ published ]
types: [published]

env:
CI: true
Expand Down
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule ".release"]
path = .release
url = git@github.com:msimerson/.release.git
1 change: 1 addition & 0 deletions .release
Submodule .release added at 0890e9
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# CHANGES

### Unreleased

### [0.6.3] - 2024-02-23

- replace mocha with node's test runner
- user: permit @ character in username
- feat(user): export login, username, & password
- feat(zone_record): add zone_record validation
- feat(zone): added zone validation
- refactor: lib/common -> lib/shared
- feat(nameserver): added NS validation
- feat: import user & group tests from v2
- add group & permission

[0.6.3]: https://github.com/NicTool/validate/releases/tag/0.6.3
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
for (const l of ['user', 'group', 'nameserver', 'zone', 'zone_record']) {
module.exports[l] = require(`./lib/${l}`)[l]
for (const l of ['group', 'user', 'nameserver', 'zone', 'zone_record']) {
module.exports[l] = require(`./lib/${l}`)
}
30 changes: 19 additions & 11 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@ const Joi = require('joi')

const shared = require('./shared')

exports.group = Joi.object({
nt_group_id: Joi.number().integer().positive(),
exports.id = Joi.number().integer().min(1).max(4294967295)

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

name: Joi.string()
.min(3)
.max(255)
.pattern(new RegExp("^[a-zA-Z0-9 _.@'-]+$"))
.pattern(new RegExp('^[a-zA-Z0-9]'))
.required(),
exports.name = Joi.string()
.min(3)
.max(255)
.pattern(new RegExp("^[a-zA-Z0-9 _.@'-]+$"))
.pattern(new RegExp('^[a-zA-Z0-9]'))

exports.v3 = Joi.object({
id: exports.id,
parent_gid: Joi.number().integer().greater(-1),
name: exports.name.required(),
deleted: Joi.boolean(),

has_children: Joi.boolean(),
permission: shared.permission,
})

// legacy group format
exports.v2 = Joi.object({
nt_group_id: exports.id,
parent_group_id: Joi.number().integer().greater(-1),
name: exports.name.required(),
deleted: Joi.boolean(),
has_children: Joi.boolean(),
permission: shared.permission,
})
55 changes: 43 additions & 12 deletions lib/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ const Joi = require('joi')
const { joiPasswordExtendCore } = require('joi-password')
const JoiPassword = Joi.extend(joiPasswordExtendCore)

const shared = require('./shared')
// const shared = require('./shared')
const group = require('./group')

exports.id = Joi.number().integer().min(1).max(4294967295)

exports.username = Joi.string()
.min(3)
.max(50)
.pattern(new RegExp('^[a-zA-Z0-9 _.-]+$', ''))
.pattern(new RegExp('^[a-zA-Z0-9 _.@-]+$', ''))

exports.password = JoiPassword.string()
.min(8)
Expand All @@ -18,29 +21,57 @@ exports.password = JoiPassword.string()
.minOfNumeric(2)
.doesNotInclude(['password', 'abc', '123', 'asdf'])

exports.login = Joi.object({
exports.email = Joi.string().lowercase().email({ minDomainSegments: 2 })

// v3 API response
exports.v3 = Joi.object({
id: exports.id,
first_name: Joi.string().min(1),
last_name: Joi.string().min(1),
username: exports.username,
password: exports.password,
email: exports.email,
is_admin: Joi.boolean(),
deleted: Joi.boolean(),
})

exports.sessionIn = Joi.object({
username: exports.username.required(),
password: exports.password.required(),
})

exports.user = Joi.object({
nt_user_id: Joi.number().integer().positive(),
nt_group_id: Joi.number().integer().positive(),
exports.sessionOut = Joi.object({
user: exports.v3,
group: Joi.object({
id: group.id,
name: group.name,
}),
session: Joi.object({
id: Joi.number().integer().min(1).max(4294967295),
last_access: Joi.number().integer().min(1).max(4294967295),
}),
meta: Joi.object(),
})

// v2 API response
exports.v2 = Joi.object({
nt_user_id: exports.id,
nt_group_id: group.id,

first_name: Joi.string().min(1).required(),
last_name: Joi.string().min(1).required(),
first_name: Joi.string().min(1),
last_name: Joi.string().min(1),

username: exports.username.required(),
password: exports.password,

email: Joi.string().lowercase().email({ minDomainSegments: 2 }).required(),
email: exports.email.required(),

is_admin: Joi.boolean(),

deleted: Joi.boolean(),

nt_user_session: Joi.string(),
last_access: Joi.string(),
// nt_user_session: Joi.string(),
// last_access: Joi.string(),

permission: shared.permission,
// permission: shared.permission,
})
18 changes: 8 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nictool/nt-validate",
"version": "0.6.1",
"name": "@nictool/validate",
"version": "0.6.3",
"description": "NicTool Object Validation",
"main": "index.js",
"directories": {
Expand All @@ -13,13 +13,13 @@
"lint:fix": "npm run lint --fix",
"prettier": "npx prettier --ignore-path .gitignore --check .",
"prettier:fix": "npx prettier --ignore-path .gitignore --write .",
"test": "npx mocha test",
"test": "test/run.sh",
"versions": "npx dependency-version-checker check",
"watch": "npm run test -- -w"
},
"repository": {
"type": "git",
"url": "git+https://github.com/NicTool/nt-validate.git"
"url": "git+https://github.com/NicTool/validate.git"
},
"keywords": [
"nictool",
Expand All @@ -30,17 +30,15 @@
"author": "Matt Simerson <matt@tnpi.net>",
"license": "BSD-3-Clause",
"bugs": {
"url": "https://github.com/NicTool/nt-validate/issues"
"url": "https://github.com/NicTool/validate/issues"
},
"homepage": "https://github.com/NicTool/nt-validate#readme",
"homepage": "https://github.com/NicTool/validate#readme",
"devDependencies": {
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"mocha": "^10.2.0",
"prettier": "^3.2.4"
"eslint-config-prettier": "^9.1.0"
},
"dependencies": {
"joi": "^17.12.1",
"joi": "^17.12.2",
"joi-password": "^4.2.0"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions test/fixtures/v3/group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "example.com",
"id": 3,
"parent_gid": 0,
"deleted": false,
"has_children": false
}
9 changes: 9 additions & 0 deletions test/fixtures/v3/nameserver.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"id": 3,
"gid": 1,
"name": "ns.tld.",
"address": "1.2.3.4",
"export_type": "djbdns",
"ttl": 3600,
"deleted": false
}
7 changes: 7 additions & 0 deletions test/fixtures/v3/user.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"username": "matt",
"password": "(t0ps3kREt)",
"email": "matt@example.com",
"first_name": "Matt",
"last_name": "Simerson"
}
13 changes: 13 additions & 0 deletions test/fixtures/v3/zone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"gid": 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
}
7 changes: 7 additions & 0 deletions test/fixtures/v3/zone_record.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"zid": 1,
"name": "a",
"ttl": 86400,
"type": "A",
"address": "1.1.1.1"
}
17 changes: 9 additions & 8 deletions test/group.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../lib/group').group
const testGroup = require('./fixtures/group.json')
const schema = require('../lib/group').v2
const testGroup = require('./fixtures/v2/group.json')

describe('group', function () {
describe('name', function () {
Expand All @@ -11,7 +12,7 @@ describe('group', function () {
const { error, value } = schema.validate(testCase)

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

it('rejects missing name', () => {
Expand All @@ -21,7 +22,7 @@ describe('group', function () {
const { error, value } = schema.validate(testCase)

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

it('rejects too short', () => {
Expand All @@ -34,7 +35,7 @@ describe('group', function () {
error.message,
'"name" length must be at least 3 characters long',
)
assert.deepStrictEqual(testCase, value)
assert.deepEqual(value, testCase)
})

it('rejects too long', () => {
Expand All @@ -51,7 +52,7 @@ describe('group', function () {
error.message,
'"name" length must be less than or equal to 255 characters long',
)
assert.deepStrictEqual(testCase, value)
assert.deepEqual(value, testCase)
})

for (const char of `~\`!$%^&*()+=[]\\/|?><":;,#{}\n`.split('')) {
Expand All @@ -65,7 +66,7 @@ describe('group', function () {
error.message,
`"name" with value "na${char}me" fails to match the required pattern: /^[a-zA-Z0-9 _.@'-]+$/`,
)
assert.deepStrictEqual(testCase, value)
assert.deepEqual(value, testCase)
})
}

Expand All @@ -80,7 +81,7 @@ describe('group', function () {
error.message,
`"name" with value "${t}" fails to match the required pattern: /^[a-zA-Z0-9]/`,
)
assert.deepStrictEqual(testCase, value)
assert.deepEqual(value, testCase)
})
}
})
Expand Down
30 changes: 30 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const assert = require('node:assert/strict')
const { describe, it } = require('node:test')

const schema = require('../index')

describe('index', function () {
const testGroup = require('./fixtures/v2/group.json')
const testUser = require('./fixtures/v2/user.json')
const testNs = require('./fixtures/v2/nameserver.json')

it('exports user', () => {
const testCase = JSON.parse(JSON.stringify(testUser))
const { error, value } = schema.user.v2.validate(testCase)
assert.ifError(error)
assert.deepEqual(testCase, value)
})

it('exports group', () => {
const testCase = JSON.parse(JSON.stringify(testGroup))
const { error, value } = schema.group.v2.validate(testCase)
assert.ifError(error)
assert.deepEqual(testCase, value)
})

it('exports nameserver', () => {
const testCase = JSON.parse(JSON.stringify(testNs))
const { error } = schema.nameserver.nameserver.validate(testCase)
assert.ifError(error)
})
})
Loading

0 comments on commit fd1edbf

Please sign in to comment.