Skip to content

Commit c2c9aab

Browse files
committed
add models, separate models into base and child
1 parent 7336dad commit c2c9aab

File tree

4 files changed

+133
-73
lines changed

4 files changed

+133
-73
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"$id": "/BaseUser",
4+
"type": "object",
5+
"title": "CVE Base User Schema",
6+
"description": "The schema for CVE Services Users",
7+
"definitions": {
8+
"uuidType": {
9+
"description": "A version 4 (random) universally unique identifier (UUID) as defined by [RFC 4122](https://tools.ietf.org/html/rfc4122#section-4.1.3).",
10+
"type": "string",
11+
"format": "uuid",
12+
"pattern": "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
13+
},
14+
"name": {
15+
"description": "User's name components",
16+
"type": "object",
17+
"required": [
18+
"first",
19+
"last"
20+
],
21+
"properties": {
22+
"first": {
23+
"type": "string",
24+
"maxLength": 100
25+
},
26+
"middle": {
27+
"type": "string",
28+
"maxLength": 100
29+
},
30+
"last": {
31+
"type": "string",
32+
"maxLength": 100
33+
},
34+
"suffix": {
35+
"type": "string",
36+
"maxLength": 100
37+
}
38+
},
39+
"additionalProperties": false
40+
}
41+
},
42+
"properties": {
43+
"name": {
44+
"$ref": "#/definitions/name"
45+
},
46+
"username": {
47+
"description": "Username should be 3-128 characters. Allowed characters are alphanumeric and -_@.",
48+
"type": "string",
49+
"minLength": 3,
50+
"maxLength": 128,
51+
"pattern": "^[A-Za-z0-9\\-_@.]{3,128}$"
52+
},
53+
"secret": {
54+
"description": "Hashed secret for user authentication",
55+
"type": "string"
56+
},
57+
"UUID": {
58+
"$ref": "#/definitions/uuidType"
59+
},
60+
"status": {
61+
"description": "User status: 'active' or 'inactive'",
62+
"type": "string",
63+
"enum": [
64+
"active",
65+
"inactive"
66+
]
67+
}
68+
},
69+
"required": [
70+
"username",
71+
"secret"
72+
],
73+
"additionalProperties": false
74+
}
Lines changed: 9 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,10 @@
11
{
2-
"$schema": "http://json-schema.org/draft-07/schema#",
3-
"$id": "/Registry-user",
4-
"type": "object",
5-
"title": "CVE Registry User Schema",
6-
"description": "The schema for CVE Services Registry Users",
7-
"definitions": {
8-
"uuidType": {
9-
"description": "A version 4 (random) universally unique identifier (UUID) as defined by [RFC 4122](https://tools.ietf.org/html/rfc4122#section-4.1.3).",
10-
"type": "string",
11-
"format": "uuid",
12-
"pattern": "^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$"
13-
},
14-
"name": {
15-
"description": "User's name components",
16-
"type": "object",
17-
"required": [
18-
"first",
19-
"last"
20-
],
21-
"properties": {
22-
"first": {
23-
"type": "string",
24-
"maxLength": 100
25-
},
26-
"middle": {
27-
"type": "string",
28-
"maxLength": 100
29-
},
30-
"last": {
31-
"type": "string",
32-
"maxLength": 100
33-
},
34-
"suffix": {
35-
"type": "string",
36-
"maxLength": 100
37-
}
38-
},
39-
"additionalProperties": false
40-
}
41-
},
42-
"properties": {
43-
"name": {
44-
"$ref": "#/definitions/name"
45-
},
46-
"username": {
47-
"description": "Username should be 3-128 characters. Allowed characters are alphanumeric and -_@.",
48-
"type": "string",
49-
"minLength": 3,
50-
"maxLength": 128,
51-
"pattern": "^[A-Za-z0-9\\-_@.]{3,128}$"
52-
},
53-
"secret": {
54-
"description": "Hashed secret for user authentication",
55-
"type": "string"
56-
},
57-
"UUID": {
58-
"$ref": "#/definitions/uuidType"
59-
},
60-
"status": {
61-
"description": "User status: 'active' or 'inactive'",
62-
"type": "string",
63-
"enum": [
64-
"active",
65-
"inactive"
66-
]
67-
}
68-
},
69-
"required": [
70-
"username",
71-
"secret"
72-
],
73-
"additionalProperties": false
74-
}
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"type": "object",
4+
"$id": "RegistryUser",
5+
"title": "CVE Registry User Schema",
6+
"description": "Schema for a CVE Registry User",
7+
"allOf": [
8+
{ "$ref": "/BaseUser" }
9+
]
10+
}

src/model/baseuser.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const mongoose = require('mongoose')
2+
const fs = require('fs')
3+
const Ajv = require('ajv')
4+
const addFormats = require('ajv-formats')
5+
6+
// Load BaseUser JSON schema
7+
const BaseUserSchemaJSON = JSON.parse(fs.readFileSync('src/middleware/schemas/BaseUser.json'))
8+
9+
// Initialize AJV
10+
const ajv = new Ajv({ allErrors: true })
11+
addFormats(ajv)
12+
13+
// Compile validation function
14+
const validate = ajv.compile(BaseUserSchemaJSON)
15+
16+
// Define Mongoose schema based on BaseUser
17+
const schema = {
18+
UUID: String,
19+
username: { type: String, required: true },
20+
secret: { type: String, required: true },
21+
name: {
22+
first: String,
23+
middle: String,
24+
last: String,
25+
suffix: String
26+
},
27+
status: { type: String, enum: ['active', 'inactive'] }
28+
}
29+
30+
// Export BaseUser model
31+
const BaseUserMongooseSchema = new mongoose.Schema(schema, {
32+
collection: 'BaseUser',
33+
timestamps: { createdAt: 'created', updatedAt: 'last_updated' }
34+
})
35+
36+
// Add validation static
37+
BaseUserMongooseSchema.statics.validateUser = function (record) {
38+
const result = { isValid: validate(record) }
39+
if (!result.isValid) result.errors = validate.errors
40+
return result
41+
}
42+
43+
const BaseUser = mongoose.model('BaseUser', BaseUserMongooseSchema)
44+
module.exports = BaseUser

src/model/registryuser.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const mongoose = require('mongoose')
2+
const BaseUser = require('./baseuser')
3+
4+
const RegistryUser = mongoose.model('RegistryUser', BaseUser.schema)
5+
6+
module.exports = RegistryUser

0 commit comments

Comments
 (0)