<commit_before>const mongoose = require('mongoose');
const Schema = require('mongoose').Schema;

// our APIs old models
const LegacyLoo = require('@neontribe/gbptm-api/src/models/loo');
const LegacyReport = require('@neontribe/gbptm-api/src/models/loo_report');

/**
 * The core schema of a loo and loo report. This is as a function because:
 * - It will give a new object instance each time, protecting against mutation
 * - There may be subtle differences in validation between reports and loos
 * This is volatile work in progress.
 */
function getCoreSchema() {
  return {
    geometry: {
      type: {
        type: String,
        required: '"{PATH}" should be "Point" and is required',
      },
      coordinates: [{ type: 'Number' }],
    },
    name: { type: String, trim: true, minlength: 1 },
    active: { type: Boolean, default: true },
    access: { type: String, default: 'public' },
    opening: { type: String, trim: true, minlength: 1 },
    type: { type: String },
    accessibleType: { type: String },
    babyChange: { type: String },
    radar: { type: String },
    attended: { type: String },
    automatic: { type: String },
    fee: { type: String, trim: true, minlength: 1 },
    notes: { type: String, trim: true, minlength: 1 },
    removal_reason: { type: String, trim: true, minlength: 1 },
    area: [
      {
        type: { type: String },
        name: { type: String },
        _id: false,
      },
    ],
  };
}

exports.Report = mongoose.model(
  'Report',
  new Schema(
    {
      previous: { type: Schema.Types.ObjectId, ref: 'Report' },
      next: { type: Schema.Types.ObjectId, ref: 'Report' },
      diff: getCoreSchema(),
    },
    { minimize: false }
  )
);
exports.LegacyLoo = LegacyLoo;
exports.LegacyReport = LegacyReport;
<commit_msg>feat: added better validation for GeoJSON points<commit_after>const mongoose = require('mongoose');
const Schema = require('mongoose').Schema;

// our APIs old models
const LegacyLoo = require('@neontribe/gbptm-api/src/models/loo');
const LegacyReport = require('@neontribe/gbptm-api/src/models/loo_report');

function ofLength(n) {
  return [list => list.length == n, `{PATH} must be of length ${n}`];
}

/**
 * The core schema of a loo and loo report. This is as a function because:
 * - It will give a new object instance each time, protecting against mutation
 * - There may be subtle differences in validation between reports and loos
 * This is volatile work in progress.
 */
function getCoreSchema() {
  return {
    geometry: {
      type: {
        type: String,
        enum: ['Point'],
        required: true,
      },
      coordinates: {
        type: [Number],
        required: true,
        validate: ofLength(2),
      },
    },
    name: { type: String, trim: true, minlength: 1 },
    active: { type: Boolean, default: true },
    access: { type: String, default: 'public' },
    opening: { type: String, trim: true, minlength: 1 },
    type: { type: String },
    accessibleType: { type: String },
    babyChange: { type: String },
    radar: { type: String },
    attended: { type: String },
    automatic: { type: String },
    fee: { type: String, trim: true, minlength: 1 },
    notes: { type: String, trim: true, minlength: 1 },
    removal_reason: { type: String, trim: true, minlength: 1 },
    area: [
      {
        type: { type: String },
        name: { type: String },
        _id: false,
      },
    ],
  };
}

const ReportSchema = new Schema(
  {
    previous: { type: Schema.Types.ObjectId, ref: 'Report' },
    next: { type: Schema.Types.ObjectId, ref: 'Report' },
    diff: getCoreSchema(),
  },
  { minimize: false }
);

exports.Report = mongoose.model('Report', ReportSchema);
exports.LegacyLoo = LegacyLoo;
exports.LegacyReport = LegacyReport;

{'fraction_non_alphanumeric': 0.14050901378579003, 'fraction_numerical': 0.0029162248144220574, 'mean_word_length': 2.922037422037422, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6588452', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1163, 'n_words': 366}