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

Attempt at validation codegen #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ dist
.pnp.*

package-lock.json
.vscode
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Struct extends ResolvedType {

this.fields = []
this.fieldsByName = new Map()
this.requiredFields = []

this.optionals = []
this.flagsPosition = -1
Expand Down Expand Up @@ -184,6 +185,8 @@ class Struct extends ResolvedType {
if (this.flagsPosition === -1) {
this.flagsPosition = i
}
} else {
this.requiredFields.push(field)
}
}
}
Expand Down Expand Up @@ -290,19 +293,19 @@ module.exports = class Hyperschema {
return json
}

toCode () {
return generateCode(this)
toCode (opts = {}) {
return generateCode(this, opts)
}

static toDisk (hyperschema, dir) {
static toDisk (hyperschema, dir, opts = {}) {
if (!dir) dir = hyperschema.dir
fs.mkdirSync(dir, { recursive: true })

const jsonPath = p.join(p.resolve(dir), JSON_FILE_NAME)
const codePath = p.join(p.resolve(dir), CODE_FILE_NAME)

fs.writeFileSync(jsonPath, JSON.stringify(hyperschema.toJSON(), null, 2), { encoding: 'utf-8' })
fs.writeFileSync(codePath, hyperschema.toCode(), { encoding: 'utf-8' })
fs.writeFileSync(codePath, hyperschema.toCode(opts), { encoding: 'utf-8' })
}

static from (json) {
Expand Down
17 changes: 16 additions & 1 deletion lib/codegen.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const gen = require('generate-object-property')
const s = require('generate-string')

module.exports = function generateSchema (hyperschema) {
module.exports = function generateSchema (hyperschema, opts = {}) {
const structs = []
const structsByName = new Map()

Expand Down Expand Up @@ -108,6 +108,9 @@ module.exports = function generateSchema (hyperschema) {
str += `// ${struct.fqn}\n`
str += `const ${id} = {\n`
str += ' preencode (state, m) {\n'
if (opts.debug) {
str += generateValidation(struct)
}
str += `${preencode}\n`
str += ' },\n'
str += ' encode (state, m) {\n'
Expand All @@ -119,6 +122,18 @@ module.exports = function generateSchema (hyperschema) {
str += '}\n'
return str

function generateValidation (struct) {
if (!struct.requiredFields.length) return ''

let str = ''
str += ' const errors = []\n'
for (const field of struct.requiredFields) {
str += ` if (${gen('m', field.name)} === undefined || ${gen('m', field.name)} === null) errors.push('${field.name}')\n`
}
str += ` if(errors.length) throw new Error(\`${struct.fqn}[\${errors.join(',')}] \${errors.length>1?'are':'is'} required\`)\n\n`
return str
}

function generateEncode (struct, { preencode = false } = {}) {
const fn = preencode ? 'preencode' : 'encode'
let str = ''
Expand Down
Loading